Refactor LogContext for IntegrationEvent Handlers to include AppName
This commit is contained in:
		
							parent
							
								
									77debf821b
								
							
						
					
					
						commit
						12148c9723
					
				| @ -24,7 +24,7 @@ namespace Basket.API.IntegrationEvents.EventHandling | ||||
| 
 | ||||
|         public async Task Handle(OrderStartedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -24,7 +24,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even | ||||
| 
 | ||||
|         public async Task Handle(ProductPriceChangedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -30,7 +30,7 @@ | ||||
| 
 | ||||
|         public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -23,7 +23,7 @@ | ||||
| 
 | ||||
|         public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -26,7 +26,7 @@ | ||||
| 
 | ||||
|         public async Task Handle(UserLocationUpdatedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -2,6 +2,7 @@ | ||||
| using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; | ||||
| using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | ||||
| using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using System.Threading; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| @ -40,7 +41,11 @@ namespace Ordering.API.Application.Commands | ||||
|     // Use for Idempotency in Command process | ||||
|     public class CancelOrderIdentifiedCommandHandler : IdentifiedCommandHandler<CancelOrderCommand, bool> | ||||
|     { | ||||
|         public CancelOrderIdentifiedCommandHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager) | ||||
|         public CancelOrderIdentifiedCommandHandler( | ||||
|             IMediator mediator, | ||||
|             IRequestManager requestManager, | ||||
|             ILogger<IdentifiedCommandHandler<CancelOrderCommand, bool>> logger) | ||||
|             : base(mediator, requestManager, logger) | ||||
|         { | ||||
|         } | ||||
| 
 | ||||
|  | ||||
| @ -1,5 +1,9 @@ | ||||
| using MediatR; | ||||
| using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using Ordering.API.Application.Behaviors; | ||||
| using Ordering.API.Application.Commands; | ||||
| using System; | ||||
| using System.Threading; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| @ -16,11 +20,16 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands | ||||
|     { | ||||
|         private readonly IMediator _mediator; | ||||
|         private readonly IRequestManager _requestManager; | ||||
|         private readonly ILogger<IdentifiedCommandHandler<T, R>> _logger; | ||||
| 
 | ||||
|         public IdentifiedCommandHandler(IMediator mediator, IRequestManager requestManager) | ||||
|         public IdentifiedCommandHandler( | ||||
|             IMediator mediator, | ||||
|             IRequestManager requestManager, | ||||
|             ILogger<IdentifiedCommandHandler<T, R>> logger) | ||||
|         { | ||||
|             _mediator = mediator; | ||||
|             _requestManager = requestManager; | ||||
|             _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
| @ -48,16 +57,60 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands | ||||
|             else | ||||
|             { | ||||
|                 await _requestManager.CreateRequestForCommandAsync<T>(message.Id); | ||||
| 				try | ||||
| 				{ | ||||
| 					// Send the embeded business command to mediator so it runs its related CommandHandler  | ||||
| 					var result = await _mediator.Send(message.Command); | ||||
| 					return result; | ||||
| 				} | ||||
| 				catch | ||||
| 				{ | ||||
| 					return default(R); | ||||
| 				} | ||||
|                 try | ||||
|                 { | ||||
|                     var command = message.Command; | ||||
|                     var commandName = command.GetGenericTypeName(); | ||||
|                     var idProperty = string.Empty; | ||||
|                     var commandId = string.Empty; | ||||
| 
 | ||||
|                     switch (command) | ||||
|                     { | ||||
|                         case CreateOrderCommand createOrderCommand: | ||||
|                             idProperty = nameof(createOrderCommand.UserId); | ||||
|                             commandId = createOrderCommand.UserId; | ||||
|                             break; | ||||
| 
 | ||||
|                         case CancelOrderCommand cancelOrderCommand: | ||||
|                             idProperty = nameof(cancelOrderCommand.OrderNumber); | ||||
|                             commandId = $"{cancelOrderCommand.OrderNumber}"; | ||||
|                             break; | ||||
| 
 | ||||
|                         case ShipOrderCommand shipOrderCommand: | ||||
|                             idProperty = nameof(shipOrderCommand.OrderNumber); | ||||
|                             commandId = $"{shipOrderCommand.OrderNumber}"; | ||||
|                             break; | ||||
| 
 | ||||
|                         default: | ||||
|                             idProperty = "Id?"; | ||||
|                             commandId = "n/a"; | ||||
|                             break; | ||||
|                     } | ||||
| 
 | ||||
|                     _logger.LogInformation( | ||||
|                         "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", | ||||
|                         commandName, | ||||
|                         idProperty, | ||||
|                         commandId, | ||||
|                         command); | ||||
| 
 | ||||
|                     // Send the embeded business command to mediator so it runs its related CommandHandler  | ||||
|                     var result = await _mediator.Send(command); | ||||
| 
 | ||||
|                     _logger.LogInformation( | ||||
|                         "----- Command result: {@Result} - {CommandName} - {IdProperty}: {CommandId} ({@Command})", | ||||
|                         result, | ||||
|                         commandName, | ||||
|                         idProperty, | ||||
|                         commandId, | ||||
|                         command); | ||||
| 
 | ||||
|                     return result; | ||||
|                 } | ||||
|                 catch | ||||
|                 { | ||||
|                     return default(R); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -33,7 +33,7 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling | ||||
|         /// <returns></returns> | ||||
|         public async Task Handle(GracePeriodConfirmedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -27,7 +27,7 @@ | ||||
| 
 | ||||
|         public async Task Handle(OrderPaymentFailedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -27,7 +27,7 @@ | ||||
| 
 | ||||
|         public async Task Handle(OrderPaymentSuccededIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -27,7 +27,7 @@ | ||||
| 
 | ||||
|         public async Task Handle(OrderStockConfirmedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -26,7 +26,7 @@ | ||||
| 
 | ||||
|         public async Task Handle(OrderStockRejectedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -34,7 +34,7 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling | ||||
|         /// <returns></returns> | ||||
|         public async Task Handle(UserCheckoutAcceptedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -26,7 +26,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling | ||||
| 
 | ||||
|         public async Task Handle(OrderStatusChangedToCancelledIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -24,7 +24,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling | ||||
| 
 | ||||
|         public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -26,7 +26,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling | ||||
| 
 | ||||
|         public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -27,7 +27,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling | ||||
| 
 | ||||
|         public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -27,7 +27,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling | ||||
| 
 | ||||
|         public async Task Handle(OrderStatusChangedToSubmittedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -25,7 +25,7 @@ namespace Ordering.SignalrHub.IntegrationEvents | ||||
| 
 | ||||
|         public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
| @ -27,7 +27,7 @@ | ||||
| 
 | ||||
|         public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event) | ||||
|         { | ||||
|             using (LogContext.PushProperty("IntegrationEventIdContext", @event.Id)) | ||||
|             using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppShortName}")) | ||||
|             { | ||||
|                 _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user