Refactor LogContext for IntegrationEvent Handlers to include AppName

This commit is contained in:
Miguel Veloso 2019-02-21 15:51:37 +00:00
parent 77debf821b
commit 12148c9723
20 changed files with 88 additions and 30 deletions

View File

@ -24,7 +24,7 @@ namespace Basket.API.IntegrationEvents.EventHandling
public async Task Handle(OrderStartedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -24,7 +24,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even
public async Task Handle(ProductPriceChangedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -30,7 +30,7 @@
public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -23,7 +23,7 @@
public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -26,7 +26,7 @@
public async Task Handle(UserLocationUpdatedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -2,6 +2,7 @@
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
using Microsoft.Extensions.Logging;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -40,7 +41,11 @@ namespace Ordering.API.Application.Commands
// Use for Idempotency in Command process // Use for Idempotency in Command process
public class CancelOrderIdentifiedCommandHandler : IdentifiedCommandHandler<CancelOrderCommand, bool> 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)
{ {
} }

View File

@ -1,5 +1,9 @@
using MediatR; using MediatR;
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; 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;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -16,11 +20,16 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
{ {
private readonly IMediator _mediator; private readonly IMediator _mediator;
private readonly IRequestManager _requestManager; 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; _mediator = mediator;
_requestManager = requestManager; _requestManager = requestManager;
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
} }
/// <summary> /// <summary>
@ -48,16 +57,60 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
else else
{ {
await _requestManager.CreateRequestForCommandAsync<T>(message.Id); await _requestManager.CreateRequestForCommandAsync<T>(message.Id);
try try
{ {
// Send the embeded business command to mediator so it runs its related CommandHandler var command = message.Command;
var result = await _mediator.Send(message.Command); var commandName = command.GetGenericTypeName();
return result; var idProperty = string.Empty;
} var commandId = string.Empty;
catch
{ switch (command)
return default(R); {
} 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);
}
} }
} }
} }

View File

@ -33,7 +33,7 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
/// <returns></returns> /// <returns></returns>
public async Task Handle(GracePeriodConfirmedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -27,7 +27,7 @@
public async Task Handle(OrderPaymentFailedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -27,7 +27,7 @@
public async Task Handle(OrderPaymentSuccededIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -27,7 +27,7 @@
public async Task Handle(OrderStockConfirmedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -26,7 +26,7 @@
public async Task Handle(OrderStockRejectedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -34,7 +34,7 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
/// <returns></returns> /// <returns></returns>
public async Task Handle(UserCheckoutAcceptedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -26,7 +26,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
public async Task Handle(OrderStatusChangedToCancelledIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -24,7 +24,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -26,7 +26,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -27,7 +27,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -27,7 +27,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
public async Task Handle(OrderStatusChangedToSubmittedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -25,7 +25,7 @@ namespace Ordering.SignalrHub.IntegrationEvents
public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);

View File

@ -27,7 +27,7 @@
public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event) 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); _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);