diff --git a/src/Services/Basket/Basket.API/Controllers/BasketController.cs b/src/Services/Basket/Basket.API/Controllers/BasketController.cs index 269f99c0d..7c1130bd6 100644 --- a/src/Services/Basket/Basket.API/Controllers/BasketController.cs +++ b/src/Services/Basket/Basket.API/Controllers/BasketController.cs @@ -25,8 +25,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers public BasketController( ILogger logger, - IBasketRepository repository, - IIdentityService identityService, + IBasketRepository repository, + IIdentityService identityService, IEventBus eventBus) { _logger = logger; @@ -58,7 +58,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers public async Task CheckoutAsync([FromBody]BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId) { var userId = _identityService.GetUserIdentity(); - + basketCheckout.RequestId = (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty) ? guid : basketCheckout.RequestId; @@ -78,20 +78,17 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers // Once basket is checkout, sends an integration event to // ordering.api to convert basket to order and proceeds with // order creation process - using (LogContext.PushProperty("IntegrationEventId", eventMessage.Id)) + try { - try - { - _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppShortName, eventMessage); + _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppShortName, eventMessage); - _eventBus.Publish(eventMessage); - } - catch (Exception ex) - { - _logger.LogError(ex, "----- ERROR Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppShortName, eventMessage); + _eventBus.Publish(eventMessage); + } + catch (Exception ex) + { + _logger.LogError(ex, "----- ERROR Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppShortName, eventMessage); - throw; - } + throw; } return Accepted(); diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/OrderStartedIntegrationEventHandler.cs b/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/OrderStartedIntegrationEventHandler.cs index 19ae1b594..a181e2b7b 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/OrderStartedIntegrationEventHandler.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/OrderStartedIntegrationEventHandler.cs @@ -1,6 +1,9 @@ using Basket.API.IntegrationEvents.Events; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.eShopOnContainers.Services.Basket.API; using Microsoft.eShopOnContainers.Services.Basket.API.Model; +using Microsoft.Extensions.Logging; +using Serilog.Context; using System; using System.Threading.Tasks; @@ -9,15 +12,24 @@ namespace Basket.API.IntegrationEvents.EventHandling public class OrderStartedIntegrationEventHandler : IIntegrationEventHandler { private readonly IBasketRepository _repository; + private readonly ILogger _logger; - public OrderStartedIntegrationEventHandler(IBasketRepository repository) + public OrderStartedIntegrationEventHandler( + IBasketRepository repository, + ILogger logger) { _repository = repository ?? throw new ArgumentNullException(nameof(repository)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(OrderStartedIntegrationEvent @event) { - await _repository.DeleteBasketAsync(@event.UserId.ToString()); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + await _repository.DeleteBasketAsync(@event.UserId.ToString()); + } } } } diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs b/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs index 9970497af..ad0c60421 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs @@ -24,7 +24,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even public async Task Handle(ProductPriceChangedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventId", @event.Id)) + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) { _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); diff --git a/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs index 416739d85..e44c6f64f 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs @@ -24,7 +24,7 @@ namespace Catalog.API.IntegrationEvents public CatalogIntegrationEventService( ILogger logger, - IEventBus eventBus, + IEventBus eventBus, CatalogContext catalogContext, Func integrationEventLogServiceFactory) { @@ -37,39 +37,33 @@ namespace Catalog.API.IntegrationEvents public async Task PublishThroughEventBusAsync(IntegrationEvent evt) { - using (LogContext.PushProperty("IntegrationEventId", evt.Id)) + try { - try - { - _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", evt.Id, Program.AppShortName, evt); + _logger.LogInformation("----- Publishing integration event: {IntegrationEventId_published} at {AppShortName} - ({@IntegrationEvent})", evt.Id, Program.AppShortName, evt); - await _eventLogService.MarkEventAsInProgressAsync(evt.Id); - _eventBus.Publish(evt); - await _eventLogService.MarkEventAsPublishedAsync(evt.Id); - } - catch (Exception ex) - { - _logger.LogError(ex, "----- ERROR Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", evt.Id, Program.AppShortName, evt); - await _eventLogService.MarkEventAsFailedAsync(evt.Id); - } + await _eventLogService.MarkEventAsInProgressAsync(evt.Id); + _eventBus.Publish(evt); + await _eventLogService.MarkEventAsPublishedAsync(evt.Id); + } + catch (Exception ex) + { + _logger.LogError(ex, "----- ERROR Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", evt.Id, Program.AppShortName, evt); + await _eventLogService.MarkEventAsFailedAsync(evt.Id); } } public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt) { - using (LogContext.PushProperty("IntegrationEventId", evt.Id)) - { - _logger.LogInformation("----- CatalogIntegrationEventService - Saving changes and integrationEvent: {IntegrationEventId}", evt.Id); + _logger.LogInformation("----- CatalogIntegrationEventService - Saving changes and integrationEvent: {IntegrationEventId}", evt.Id); - //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction(): - //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency - await ResilientTransaction.New(_catalogContext).ExecuteAsync(async () => - { - // Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction - await _catalogContext.SaveChangesAsync(); - await _eventLogService.SaveEventAsync(evt, _catalogContext.Database.CurrentTransaction.GetDbTransaction()); - }); - } + //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction(): + //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency + await ResilientTransaction.New(_catalogContext).ExecuteAsync(async () => + { + // Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction + await _catalogContext.SaveChangesAsync(); + await _eventLogService.SaveEventAsync(evt, _catalogContext.Database.CurrentTransaction.GetDbTransaction()); + }); } } } diff --git a/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs index 0f30a3e0a..fb1ae88f3 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs @@ -8,39 +8,51 @@ using System.Linq; using global::Catalog.API.IntegrationEvents; using IntegrationEvents.Events; + using Serilog.Context; + using Microsoft.Extensions.Logging; public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler : IIntegrationEventHandler { private readonly CatalogContext _catalogContext; private readonly ICatalogIntegrationEventService _catalogIntegrationEventService; + private readonly ILogger _logger; - public OrderStatusChangedToAwaitingValidationIntegrationEventHandler(CatalogContext catalogContext, - ICatalogIntegrationEventService catalogIntegrationEventService) + public OrderStatusChangedToAwaitingValidationIntegrationEventHandler( + CatalogContext catalogContext, + ICatalogIntegrationEventService catalogIntegrationEventService, + ILogger logger) { _catalogContext = catalogContext; _catalogIntegrationEventService = catalogIntegrationEventService; + _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); } - public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent command) + public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event) { - var confirmedOrderStockItems = new List(); - - foreach (var orderStockItem in command.OrderStockItems) + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) { - var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId); - var hasStock = catalogItem.AvailableStock >= orderStockItem.Units; - var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id, hasStock); + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + var confirmedOrderStockItems = new List(); + + foreach (var orderStockItem in @event.OrderStockItems) + { + var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId); + var hasStock = catalogItem.AvailableStock >= orderStockItem.Units; + var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id, hasStock); + + confirmedOrderStockItems.Add(confirmedOrderStockItem); + } + + var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.HasStock) + ? (IntegrationEvent)new OrderStockRejectedIntegrationEvent(@event.OrderId, confirmedOrderStockItems) + : new OrderStockConfirmedIntegrationEvent(@event.OrderId); + + await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent); + await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent); - confirmedOrderStockItems.Add(confirmedOrderStockItem); } - - var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.HasStock) - ? (IntegrationEvent) new OrderStockRejectedIntegrationEvent(command.OrderId, confirmedOrderStockItems) - : new OrderStockConfirmedIntegrationEvent(command.OrderId); - - await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent); - await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent); } } } \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs index 0a45547f9..7d3ffc99a 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs @@ -4,28 +4,40 @@ using System.Threading.Tasks; using Infrastructure; using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events; + using Microsoft.Extensions.Logging; + using Serilog.Context; public class OrderStatusChangedToPaidIntegrationEventHandler : IIntegrationEventHandler { private readonly CatalogContext _catalogContext; + private readonly ILogger _logger; - public OrderStatusChangedToPaidIntegrationEventHandler(CatalogContext catalogContext) + public OrderStatusChangedToPaidIntegrationEventHandler( + CatalogContext catalogContext, + ILogger logger) { _catalogContext = catalogContext; + _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); } - public async Task Handle(OrderStatusChangedToPaidIntegrationEvent command) + public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) { - //we're not blocking stock/inventory - foreach (var orderStockItem in command.OrderStockItems) + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) { - var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId); + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + //we're not blocking stock/inventory + foreach (var orderStockItem in @event.OrderStockItems) + { + var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId); + + catalogItem.RemoveStock(orderStockItem.Units); + } + + await _catalogContext.SaveChangesAsync(); - catalogItem.RemoveStock(orderStockItem.Units); } - - await _catalogContext.SaveChangesAsync(); } } } \ No newline at end of file diff --git a/src/Services/Marketing/Marketing.API/IntegrationEvents/Handlers/UserLocationUpdatedIntegrationEventHandler.cs b/src/Services/Marketing/Marketing.API/IntegrationEvents/Handlers/UserLocationUpdatedIntegrationEventHandler.cs index 77574c0e5..071393f62 100644 --- a/src/Services/Marketing/Marketing.API/IntegrationEvents/Handlers/UserLocationUpdatedIntegrationEventHandler.cs +++ b/src/Services/Marketing/Marketing.API/IntegrationEvents/Handlers/UserLocationUpdatedIntegrationEventHandler.cs @@ -4,6 +4,8 @@ using Marketing.API.Model; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Repositories; + using Microsoft.Extensions.Logging; + using Serilog.Context; using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -12,20 +14,29 @@ : IIntegrationEventHandler { private readonly IMarketingDataRepository _marketingDataRepository; + private readonly ILogger _logger; - public UserLocationUpdatedIntegrationEventHandler(IMarketingDataRepository repository) + public UserLocationUpdatedIntegrationEventHandler( + IMarketingDataRepository repository, + ILogger logger) { _marketingDataRepository = repository ?? throw new ArgumentNullException(nameof(repository)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(UserLocationUpdatedIntegrationEvent @event) { - var userMarketingData = await _marketingDataRepository.GetAsync(@event.UserId); - userMarketingData = userMarketingData ?? - new MarketingData() { UserId = @event.UserId }; + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); - userMarketingData.Locations = MapUpdatedUserLocations(@event.LocationList); - await _marketingDataRepository.UpdateLocationAsync(userMarketingData); + var userMarketingData = await _marketingDataRepository.GetAsync(@event.UserId); + userMarketingData = userMarketingData ?? + new MarketingData() { UserId = @event.UserId }; + + userMarketingData.Locations = MapUpdatedUserLocations(@event.LocationList); + await _marketingDataRepository.UpdateLocationAsync(userMarketingData); + } } private List MapUpdatedUserLocations(List newUserLocations) diff --git a/src/Services/Marketing/Marketing.API/Program.cs b/src/Services/Marketing/Marketing.API/Program.cs index 2735d0177..f0f96eecc 100644 --- a/src/Services/Marketing/Marketing.API/Program.cs +++ b/src/Services/Marketing/Marketing.API/Program.cs @@ -13,7 +13,7 @@ public class Program { public static readonly string AppName = typeof(Program).Namespace; - public static readonly string ShortAppName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1); + public static readonly string AppShortName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1); public static int Main(string[] args) { diff --git a/src/Services/Ordering/Ordering.API/Application/Behaviors/BehaviorsHelperExtensions.cs b/src/Services/Ordering/Ordering.API/Application/Behaviors/BehaviorsHelperExtensions.cs index 1c44df90a..7374ff164 100644 --- a/src/Services/Ordering/Ordering.API/Application/Behaviors/BehaviorsHelperExtensions.cs +++ b/src/Services/Ordering/Ordering.API/Application/Behaviors/BehaviorsHelperExtensions.cs @@ -7,7 +7,7 @@ namespace Ordering.API.Application.Behaviors { internal static class BehaviorsHelperExtensions { - internal static string GetTypeName(this object @object) + internal static string GetGenericTypeName(this object @object) { var typeName = string.Empty; var type = @object.GetType(); diff --git a/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs b/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs index b43cd942f..70eaa724c 100644 --- a/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs +++ b/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs @@ -13,9 +13,9 @@ namespace Ordering.API.Application.Behaviors public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) { - _logger.LogInformation("----- Handling command {CommandName} ({@Command})", request.GetTypeName(), request); + _logger.LogInformation("----- Handling command {CommandName} ({@Command})", request.GetGenericTypeName(), request); var response = await next(); - _logger.LogInformation("----- Command {CommandName} handled - response: {@Response}", request.GetTypeName(), response); + _logger.LogInformation("----- Command {CommandName} handled - response: {@Response}", request.GetGenericTypeName(), response); return response; } diff --git a/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs b/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs index 0c40faa4d..49a325477 100644 --- a/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs +++ b/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs @@ -27,7 +27,7 @@ namespace Ordering.API.Application.Behaviors public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) { TResponse response = default(TResponse); - var typeName = request.GetTypeName(); + var typeName = request.GetGenericTypeName(); try { diff --git a/src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs b/src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs index d40b105d0..ede51e463 100644 --- a/src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs +++ b/src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs @@ -21,7 +21,7 @@ namespace Ordering.API.Application.Behaviors public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) { - var typeName = request.GetTypeName(); + var typeName = request.GetGenericTypeName(); _logger.LogInformation("----- Validating command {CommandType}", typeName); diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/GracePeriodConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/GracePeriodConfirmedIntegrationEventHandler.cs index f8dcc6edb..f98db6ece 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/GracePeriodConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/GracePeriodConfirmedIntegrationEventHandler.cs @@ -1,8 +1,11 @@ using MediatR; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.eShopOnContainers.Services.Ordering.API; using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; +using Microsoft.Extensions.Logging; using Ordering.API.Application.Commands; using Ordering.API.Application.IntegrationEvents.Events; +using Serilog.Context; using System.Threading.Tasks; namespace Ordering.API.Application.IntegrationEvents.EventHandling @@ -10,10 +13,14 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling public class GracePeriodConfirmedIntegrationEventHandler : IIntegrationEventHandler { private readonly IMediator _mediator; + private readonly ILogger _logger; - public GracePeriodConfirmedIntegrationEventHandler(IMediator mediator) + public GracePeriodConfirmedIntegrationEventHandler( + IMediator mediator, + ILogger logger) { _mediator = mediator; + _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); } /// @@ -26,8 +33,13 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling /// public async Task Handle(GracePeriodConfirmedIntegrationEvent @event) { - var command = new SetAwaitingValidationOrderStatusCommand(@event.OrderId); - await _mediator.Send(command); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + var command = new SetAwaitingValidationOrderStatusCommand(@event.OrderId); + await _mediator.Send(command); + } } } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs index 5f4fc28e1..1767c6a71 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs @@ -2,9 +2,12 @@ { using MediatR; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; + using Microsoft.eShopOnContainers.Services.Ordering.API; using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; + using Microsoft.Extensions.Logging; using Ordering.API.Application.Commands; using Ordering.API.Application.IntegrationEvents.Events; + using Serilog.Context; using System; using System.Threading.Tasks; @@ -12,16 +15,25 @@ IIntegrationEventHandler { private readonly IMediator _mediator; + private readonly ILogger _logger; - public OrderPaymentFailedIntegrationEventHandler(IMediator mediator) + public OrderPaymentFailedIntegrationEventHandler( + IMediator mediator, + ILogger logger) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(OrderPaymentFailedIntegrationEvent @event) { - var command = new CancelOrderCommand(@event.OrderId); - await _mediator.Send(command); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + var command = new CancelOrderCommand(@event.OrderId); + await _mediator.Send(command); + } } } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs index 6c201d77e..0077faf82 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs @@ -2,9 +2,12 @@ { using MediatR; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; + using Microsoft.eShopOnContainers.Services.Ordering.API; using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; + using Microsoft.Extensions.Logging; using Ordering.API.Application.Commands; using Ordering.API.Application.IntegrationEvents.Events; + using Serilog.Context; using System; using System.Threading.Tasks; @@ -12,16 +15,25 @@ IIntegrationEventHandler { private readonly IMediator _mediator; + private readonly ILogger _logger; - public OrderPaymentSuccededIntegrationEventHandler(IMediator mediator) + public OrderPaymentSuccededIntegrationEventHandler( + IMediator mediator, + ILogger logger) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(OrderPaymentSuccededIntegrationEvent @event) { - var command = new SetPaidOrderStatusCommand(@event.OrderId); - await _mediator.Send(command); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + var command = new SetPaidOrderStatusCommand(@event.OrderId); + await _mediator.Send(command); + } } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs index c5561508b..9785a4cd0 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs @@ -7,21 +7,33 @@ using MediatR; using System; using Ordering.API.Application.Commands; + using Microsoft.Extensions.Logging; + using Serilog.Context; + using Microsoft.eShopOnContainers.Services.Ordering.API; - public class OrderStockConfirmedIntegrationEventHandler : + public class OrderStockConfirmedIntegrationEventHandler : IIntegrationEventHandler { private readonly IMediator _mediator; + private readonly ILogger _logger; - public OrderStockConfirmedIntegrationEventHandler(IMediator mediator) + public OrderStockConfirmedIntegrationEventHandler( + IMediator mediator, + ILogger logger) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(OrderStockConfirmedIntegrationEvent @event) { - var command = new SetStockConfirmedOrderStatusCommand(@event.OrderId); - await _mediator.Send(command); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + var command = new SetStockConfirmedOrderStatusCommand(@event.OrderId); + await _mediator.Send(command); + } } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs index af7d98f74..2fc9ddb1c 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs @@ -7,25 +7,37 @@ using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using MediatR; using Ordering.API.Application.Commands; + using Microsoft.Extensions.Logging; + using Serilog.Context; + using Microsoft.eShopOnContainers.Services.Ordering.API; public class OrderStockRejectedIntegrationEventHandler : IIntegrationEventHandler { private readonly IMediator _mediator; + private readonly ILogger _logger; - public OrderStockRejectedIntegrationEventHandler(IMediator mediator) + public OrderStockRejectedIntegrationEventHandler( + IMediator mediator, + ILogger logger) { _mediator = mediator; + _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); } public async Task Handle(OrderStockRejectedIntegrationEvent @event) { - var orderStockRejectedItems = @event.OrderStockItems - .FindAll(c => !c.HasStock) - .Select(c => c.ProductId) - .ToList(); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); - var command = new SetStockRejectedOrderStatusCommand(@event.OrderId, orderStockRejectedItems); - await _mediator.Send(command); + var orderStockRejectedItems = @event.OrderStockItems + .FindAll(c => !c.HasStock) + .Select(c => c.ProductId) + .ToList(); + + var command = new SetStockRejectedOrderStatusCommand(@event.OrderId, orderStockRejectedItems); + await _mediator.Send(command); + } } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs index 457b116cb..d0f52befa 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs @@ -26,30 +26,30 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling /// /// Integration event handler which starts the create order process /// - /// + /// /// Integration event message which is sent by the /// basket.api once it has successfully process the /// order items. /// /// - public async Task Handle(UserCheckoutAcceptedIntegrationEvent eventMsg) + public async Task Handle(UserCheckoutAcceptedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventId", eventMsg.Id)) + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", eventMsg.Id, Program.AppShortName, eventMsg); + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); var result = false; - if (eventMsg.RequestId != Guid.Empty) + if (@event.RequestId != Guid.Empty) { - using (LogContext.PushProperty("IdentifiedCommandId", eventMsg.RequestId)) + using (LogContext.PushProperty("IdentifiedCommandId", @event.RequestId)) { - var createOrderCommand = new CreateOrderCommand(eventMsg.Basket.Items, eventMsg.UserId, eventMsg.UserName, eventMsg.City, eventMsg.Street, - eventMsg.State, eventMsg.Country, eventMsg.ZipCode, - eventMsg.CardNumber, eventMsg.CardHolderName, eventMsg.CardExpiration, - eventMsg.CardSecurityNumber, eventMsg.CardTypeId); + var createOrderCommand = new CreateOrderCommand(@event.Basket.Items, @event.UserId, @event.UserName, @event.City, @event.Street, + @event.State, @event.Country, @event.ZipCode, + @event.CardNumber, @event.CardHolderName, @event.CardExpiration, + @event.CardSecurityNumber, @event.CardTypeId); - var requestCreateOrder = new IdentifiedCommand(createOrderCommand, eventMsg.RequestId); + var requestCreateOrder = new IdentifiedCommand(createOrderCommand, @event.RequestId); _logger.LogInformation("----- IdentifiedCreateOrderCommand: {@IdentifiedCreateOrderCommand}", requestCreateOrder); @@ -57,17 +57,17 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling if (result) { - _logger.LogInformation("----- CreateOrderCommand suceeded - RequestId: {RequestId}", eventMsg.RequestId); + _logger.LogInformation("----- CreateOrderCommand suceeded - RequestId: {RequestId}", @event.RequestId); } else { - _logger.LogWarning("----- CreateOrderCommand failed - RequestId: {RequestId}", eventMsg.RequestId); + _logger.LogWarning("----- CreateOrderCommand failed - RequestId: {RequestId}", @event.RequestId); } } } else { - _logger.LogWarning("----- Invalid IntegrationEvent - RequestId is missing - {@IntegrationEvent}", eventMsg); + _logger.LogWarning("----- Invalid IntegrationEvent - RequestId is missing - {@IntegrationEvent}", @event); } } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs index 5bae18beb..95adfd64d 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs @@ -5,6 +5,7 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Utilities; +using Microsoft.eShopOnContainers.Services.Ordering.API; using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; using Microsoft.Extensions.Logging; using System; @@ -44,7 +45,7 @@ namespace Ordering.API.Application.IntegrationEvents foreach (var logEvt in pendindLogEvents) { - _logger.LogInformation("----- Publishing integration event {IntegrationEventId} ({@IntegrationEvent})", logEvt.EventId, logEvt); + _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", logEvt.EventId, Program.AppShortName, logEvt); try { diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs index 171eb81f7..4efc07e4d 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.Extensions.Logging; using Ordering.SignalrHub.IntegrationEvents.Events; +using Serilog.Context; using System; using System.Collections.Generic; using System.Linq; @@ -11,18 +13,27 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling public class OrderStatusChangedToCancelledIntegrationEventHandler : IIntegrationEventHandler { private readonly IHubContext _hubContext; + private readonly ILogger _logger; - public OrderStatusChangedToCancelledIntegrationEventHandler(IHubContext hubContext) + public OrderStatusChangedToCancelledIntegrationEventHandler( + IHubContext hubContext, + ILogger logger) { _hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(OrderStatusChangedToCancelledIntegrationEvent @event) { - await _hubContext.Clients - .Group(@event.BuyerName) - .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + await _hubContext.Clients + .Group(@event.BuyerName) + .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + } } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs index 4b0eb780c..df7ce5cf9 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.Extensions.Logging; using Ordering.SignalrHub.IntegrationEvents.Events; +using Serilog.Context; using System; using System.Threading.Tasks; @@ -9,18 +11,27 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling public class OrderStatusChangedToPaidIntegrationEventHandler : IIntegrationEventHandler { private readonly IHubContext _hubContext; + private readonly ILogger _logger; - public OrderStatusChangedToPaidIntegrationEventHandler(IHubContext hubContext) + public OrderStatusChangedToPaidIntegrationEventHandler( + IHubContext hubContext, + ILogger logger) { _hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) { - await _hubContext.Clients - .Group(@event.BuyerName) - .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + await _hubContext.Clients + .Group(@event.BuyerName) + .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + } } } } diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs index 7a19a0659..99a06f57b 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.Extensions.Logging; using Ordering.SignalrHub.IntegrationEvents.Events; +using Serilog.Context; using System; using System.Collections.Generic; using System.Linq; @@ -11,18 +13,27 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling public class OrderStatusChangedToShippedIntegrationEventHandler : IIntegrationEventHandler { private readonly IHubContext _hubContext; + private readonly ILogger _logger; - public OrderStatusChangedToShippedIntegrationEventHandler(IHubContext hubContext) + public OrderStatusChangedToShippedIntegrationEventHandler( + IHubContext hubContext, + ILogger logger) { _hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event) { - await _hubContext.Clients - .Group(@event.BuyerName) - .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + await _hubContext.Clients + .Group(@event.BuyerName) + .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + } } } } diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs index 324ea6259..4997da16a 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.Extensions.Logging; using Ordering.SignalrHub.IntegrationEvents.Events; +using Serilog.Context; using System; using System.Collections.Generic; using System.Linq; @@ -12,18 +14,27 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling IIntegrationEventHandler { private readonly IHubContext _hubContext; + private readonly ILogger _logger; - public OrderStatusChangedToStockConfirmedIntegrationEventHandler(IHubContext hubContext) + public OrderStatusChangedToStockConfirmedIntegrationEventHandler( + IHubContext hubContext, + ILogger logger) { _hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event) { - await _hubContext.Clients - .Group(@event.BuyerName) - .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + await _hubContext.Clients + .Group(@event.BuyerName) + .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + } } } } diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs index 5fac4c1da..59e840528 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.Extensions.Logging; using Ordering.SignalrHub.IntegrationEvents.Events; +using Serilog.Context; using System; using System.Collections.Generic; using System.Linq; @@ -12,18 +14,27 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling IIntegrationEventHandler { private readonly IHubContext _hubContext; + private readonly ILogger _logger; - public OrderStatusChangedToSubmittedIntegrationEventHandler(IHubContext hubContext) + public OrderStatusChangedToSubmittedIntegrationEventHandler( + IHubContext hubContext, + ILogger logger) { _hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(OrderStatusChangedToSubmittedIntegrationEvent @event) { - await _hubContext.Clients - .Group(@event.BuyerName) - .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + await _hubContext.Clients + .Group(@event.BuyerName) + .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + } } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/orderStatusChangedToAwaitingValidationIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/orderStatusChangedToAwaitingValidationIntegrationEventHandler.cs index 6c2733b77..00860e2ef 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/orderStatusChangedToAwaitingValidationIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/orderStatusChangedToAwaitingValidationIntegrationEventHandler.cs @@ -1,5 +1,7 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.Extensions.Logging; +using Serilog.Context; using System; using System.Collections.Generic; using System.Linq; @@ -10,18 +12,27 @@ namespace Ordering.SignalrHub.IntegrationEvents public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler : IIntegrationEventHandler { private readonly IHubContext _hubContext; + private readonly ILogger _logger; - public OrderStatusChangedToAwaitingValidationIntegrationEventHandler(IHubContext hubContext) + public OrderStatusChangedToAwaitingValidationIntegrationEventHandler( + IHubContext hubContext, + ILogger logger) { _hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event) { - await _hubContext.Clients - .Group(@event.BuyerName) - .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); + + await _hubContext.Clients + .Group(@event.BuyerName) + .SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); + } } } } diff --git a/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs b/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs index c70a32093..0f3324ca0 100644 --- a/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs @@ -2,45 +2,56 @@ { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Payment.API.IntegrationEvents.Events; + using Serilog.Context; using System.Threading.Tasks; - public class OrderStatusChangedToStockConfirmedIntegrationEventHandler : + public class OrderStatusChangedToStockConfirmedIntegrationEventHandler : IIntegrationEventHandler { private readonly IEventBus _eventBus; private readonly PaymentSettings _settings; + private readonly ILogger _logger; - public OrderStatusChangedToStockConfirmedIntegrationEventHandler(IEventBus eventBus, - IOptionsSnapshot settings) + public OrderStatusChangedToStockConfirmedIntegrationEventHandler( + IEventBus eventBus, + IOptionsSnapshot settings, + ILogger logger) { _eventBus = eventBus; _settings = settings.Value; - } + _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); + } public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event) { - IntegrationEvent orderPaymentIntegrationEvent; - - //Business feature comment: - // When OrderStatusChangedToStockConfirmed Integration Event is handled. - // Here we're simulating that we'd be performing the payment against any payment gateway - // Instead of a real payment we just take the env. var to simulate the payment - // The payment can be successful or it can fail - - if (_settings.PaymentSucceded) + using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id)) { - orderPaymentIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId); - } - else - { - orderPaymentIntegrationEvent = new OrderPaymentFailedIntegrationEvent(@event.OrderId); - } + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event); - _eventBus.Publish(orderPaymentIntegrationEvent); + IntegrationEvent orderPaymentIntegrationEvent; - await Task.CompletedTask; + //Business feature comment: + // When OrderStatusChangedToStockConfirmed Integration Event is handled. + // Here we're simulating that we'd be performing the payment against any payment gateway + // Instead of a real payment we just take the env. var to simulate the payment + // The payment can be successful or it can fail + + if (_settings.PaymentSucceded) + { + orderPaymentIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId); + } + else + { + orderPaymentIntegrationEvent = new OrderPaymentFailedIntegrationEvent(@event.OrderId); + } + + _eventBus.Publish(orderPaymentIntegrationEvent); + + await Task.CompletedTask; + } } } } \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/Program.cs b/src/Services/Payment/Payment.API/Program.cs index bd1f65322..f5a0065c7 100644 --- a/src/Services/Payment/Payment.API/Program.cs +++ b/src/Services/Payment/Payment.API/Program.cs @@ -12,7 +12,7 @@ namespace Payment.API public class Program { public static readonly string AppName = typeof(Program).Namespace; - public static readonly string ShortAppName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1); + public static readonly string AppShortName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1); public static int Main(string[] args) {