diff --git a/src/Services/Basket/Basket.API/Infrastructure/Repositories/RedisBasketRepository.cs b/src/Services/Basket/Basket.API/Infrastructure/Repositories/RedisBasketRepository.cs index 7db463f20..1834fd2cc 100644 --- a/src/Services/Basket/Basket.API/Infrastructure/Repositories/RedisBasketRepository.cs +++ b/src/Services/Basket/Basket.API/Infrastructure/Repositories/RedisBasketRepository.cs @@ -1,4 +1,4 @@ -namespace Microsoft.eShopOnContainers.Services.Basket.API.Infrastructure.Repositories; +namespace Microsoft.eShopOnContainers.Services.Basket.API.Infrastructure.Repositories; public class RedisBasketRepository : IBasketRepository { @@ -6,9 +6,9 @@ public class RedisBasketRepository : IBasketRepository private readonly ConnectionMultiplexer _redis; private readonly IDatabase _database; - public RedisBasketRepository(ILoggerFactory loggerFactory, ConnectionMultiplexer redis) + public RedisBasketRepository(ILogger logger, ConnectionMultiplexer redis) { - _logger = loggerFactory.CreateLogger(); + _logger = logger; _redis = redis; _database = redis.GetDatabase(); } diff --git a/src/Services/Basket/Basket.FunctionalTests/RedisBasketRepositoryTests.cs b/src/Services/Basket/Basket.FunctionalTests/RedisBasketRepositoryTests.cs index 835146c2e..540b01ccd 100644 --- a/src/Services/Basket/Basket.FunctionalTests/RedisBasketRepositoryTests.cs +++ b/src/Services/Basket/Basket.FunctionalTests/RedisBasketRepositoryTests.cs @@ -1,5 +1,7 @@  +using Microsoft.Extensions.Logging.Abstractions; + namespace Basket.FunctionalTests { public class RedisBasketRepositoryTests @@ -48,8 +50,8 @@ namespace Basket.FunctionalTests RedisBasketRepository BuildBasketRepository(ConnectionMultiplexer connMux) { - var loggerFactory = new LoggerFactory(); - return new RedisBasketRepository(loggerFactory, connMux); + var logger = NullLoggerFactory.Instance.CreateLogger(); + return new RedisBasketRepository(logger, connMux); } List BuildBasketItems() diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderCancelledDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderCancelledDomainEventHandler.cs index e53a233d9..dc9944c83 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderCancelledDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderCancelledDomainEventHandler.cs @@ -1,16 +1,16 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.DomainEventHandlers; -public class OrderCancelledDomainEventHandler +public partial class OrderCancelledDomainEventHandler : INotificationHandler { private readonly IOrderRepository _orderRepository; private readonly IBuyerRepository _buyerRepository; - private readonly ILoggerFactory _logger; + private readonly ILogger _logger; private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; public OrderCancelledDomainEventHandler( IOrderRepository orderRepository, - ILoggerFactory logger, + ILogger logger, IBuyerRepository buyerRepository, IOrderingIntegrationEventService orderingIntegrationEventService) { @@ -20,16 +20,14 @@ public class OrderCancelledDomainEventHandler _orderingIntegrationEventService = orderingIntegrationEventService; } - public async Task Handle(OrderCancelledDomainEvent orderCancelledDomainEvent, CancellationToken cancellationToken) + public async Task Handle(OrderCancelledDomainEvent domainEvent, CancellationToken cancellationToken) { - _logger.CreateLogger() - .LogTrace("Order with Id: {OrderId} has been successfully updated to status {Status} ({Id})", - orderCancelledDomainEvent.Order.Id, nameof(OrderStatus.Cancelled), OrderStatus.Cancelled.Id); + OrderingApiTrace.LogOrderStatusUpdated(_logger, domainEvent.Order.Id, nameof(OrderStatus.Cancelled), OrderStatus.Cancelled.Id); - var order = await _orderRepository.GetAsync(orderCancelledDomainEvent.Order.Id); + var order = await _orderRepository.GetAsync(domainEvent.Order.Id); var buyer = await _buyerRepository.FindByIdAsync(order.GetBuyerId.Value.ToString()); - var orderStatusChangedToCancelledIntegrationEvent = new OrderStatusChangedToCancelledIntegrationEvent(order.Id, order.OrderStatus.Name, buyer.Name); - await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedToCancelledIntegrationEvent); + var integrationEvent = new OrderStatusChangedToCancelledIntegrationEvent(order.Id, order.OrderStatus.Name, buyer.Name); + await _orderingIntegrationEventService.AddAndSaveEventAsync(integrationEvent); } } diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderShippedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderShippedDomainEventHandler.cs index aba9d8923..3bdb035e7 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderShippedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderShippedDomainEventHandler.cs @@ -6,11 +6,11 @@ public class OrderShippedDomainEventHandler private readonly IOrderRepository _orderRepository; private readonly IBuyerRepository _buyerRepository; private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; - private readonly ILoggerFactory _logger; + private readonly ILogger _logger; public OrderShippedDomainEventHandler( IOrderRepository orderRepository, - ILoggerFactory logger, + ILogger logger, IBuyerRepository buyerRepository, IOrderingIntegrationEventService orderingIntegrationEventService) { @@ -20,16 +20,14 @@ public class OrderShippedDomainEventHandler _orderingIntegrationEventService = orderingIntegrationEventService; } - public async Task Handle(OrderShippedDomainEvent orderShippedDomainEvent, CancellationToken cancellationToken) + public async Task Handle(OrderShippedDomainEvent domainEvent, CancellationToken cancellationToken) { - _logger.CreateLogger() - .LogTrace("Order with Id: {OrderId} has been successfully updated to status {Status} ({Id})", - orderShippedDomainEvent.Order.Id, nameof(OrderStatus.Shipped), OrderStatus.Shipped.Id); + OrderingApiTrace.LogOrderStatusUpdated(_logger, domainEvent.Order.Id, nameof(OrderStatus.Shipped), OrderStatus.Shipped.Id); - var order = await _orderRepository.GetAsync(orderShippedDomainEvent.Order.Id); + var order = await _orderRepository.GetAsync(domainEvent.Order.Id); var buyer = await _buyerRepository.FindByIdAsync(order.GetBuyerId.Value.ToString()); - var orderStatusChangedToShippedIntegrationEvent = new OrderStatusChangedToShippedIntegrationEvent(order.Id, order.OrderStatus.Name, buyer.Name); - await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedToShippedIntegrationEvent); + var integrationEvent = new OrderStatusChangedToShippedIntegrationEvent(order.Id, order.OrderStatus.Name, buyer.Name); + await _orderingIntegrationEventService.AddAndSaveEventAsync(integrationEvent); } } diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs index e280d06c3..4b8553f50 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs @@ -4,12 +4,13 @@ public class OrderStatusChangedToAwaitingValidationDomainEventHandler : INotificationHandler { private readonly IOrderRepository _orderRepository; - private readonly ILoggerFactory _logger; + private readonly ILogger _logger; private readonly IBuyerRepository _buyerRepository; private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; public OrderStatusChangedToAwaitingValidationDomainEventHandler( - IOrderRepository orderRepository, ILoggerFactory logger, + IOrderRepository orderRepository, + ILogger logger, IBuyerRepository buyerRepository, IOrderingIntegrationEventService orderingIntegrationEventService) { @@ -19,21 +20,17 @@ public class OrderStatusChangedToAwaitingValidationDomainEventHandler _orderingIntegrationEventService = orderingIntegrationEventService; } - public async Task Handle(OrderStatusChangedToAwaitingValidationDomainEvent orderStatusChangedToAwaitingValidationDomainEvent, CancellationToken cancellationToken) + public async Task Handle(OrderStatusChangedToAwaitingValidationDomainEvent domainEvent, CancellationToken cancellationToken) { - _logger.CreateLogger() - .LogTrace("Order with Id: {OrderId} has been successfully updated to status {Status} ({Id})", - orderStatusChangedToAwaitingValidationDomainEvent.OrderId, nameof(OrderStatus.AwaitingValidation), OrderStatus.AwaitingValidation.Id); - - var order = await _orderRepository.GetAsync(orderStatusChangedToAwaitingValidationDomainEvent.OrderId); + OrderingApiTrace.LogOrderStatusUpdated(_logger, domainEvent.OrderId, nameof(OrderStatus.AwaitingValidation), OrderStatus.AwaitingValidation.Id); + var order = await _orderRepository.GetAsync(domainEvent.OrderId); var buyer = await _buyerRepository.FindByIdAsync(order.GetBuyerId.Value.ToString()); - var orderStockList = orderStatusChangedToAwaitingValidationDomainEvent.OrderItems + var orderStockList = domainEvent.OrderItems .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); - var orderStatusChangedToAwaitingValidationIntegrationEvent = new OrderStatusChangedToAwaitingValidationIntegrationEvent( - order.Id, order.OrderStatus.Name, buyer.Name, orderStockList); - await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedToAwaitingValidationIntegrationEvent); + var integrationEvent = new OrderStatusChangedToAwaitingValidationIntegrationEvent(order.Id, order.OrderStatus.Name, buyer.Name, orderStockList); + await _orderingIntegrationEventService.AddAndSaveEventAsync(integrationEvent); } } diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToPaidDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToPaidDomainEventHandler.cs index e5cc83a75..761313a4d 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToPaidDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToPaidDomainEventHandler.cs @@ -1,17 +1,15 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.DomainEventHandlers; -public class OrderStatusChangedToPaidDomainEventHandler - : INotificationHandler +public class OrderStatusChangedToPaidDomainEventHandler : INotificationHandler { private readonly IOrderRepository _orderRepository; - private readonly ILoggerFactory _logger; + private readonly ILogger _logger; private readonly IBuyerRepository _buyerRepository; private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; - public OrderStatusChangedToPaidDomainEventHandler( IOrderRepository orderRepository, - ILoggerFactory logger, + ILogger logger, IBuyerRepository buyerRepository, IOrderingIntegrationEventService orderingIntegrationEventService) { @@ -21,24 +19,22 @@ public class OrderStatusChangedToPaidDomainEventHandler _orderingIntegrationEventService = orderingIntegrationEventService ?? throw new ArgumentNullException(nameof(orderingIntegrationEventService)); } - public async Task Handle(OrderStatusChangedToPaidDomainEvent orderStatusChangedToPaidDomainEvent, CancellationToken cancellationToken) + public async Task Handle(OrderStatusChangedToPaidDomainEvent domainEvent, CancellationToken cancellationToken) { - _logger.CreateLogger() - .LogInformation("Order with Id: {OrderId} has been successfully updated to status {Status} ({Id})", - orderStatusChangedToPaidDomainEvent.OrderId, nameof(OrderStatus.Paid), OrderStatus.Paid.Id); + OrderingApiTrace.LogOrderStatusUpdated(_logger, domainEvent.OrderId, nameof(OrderStatus.Paid), OrderStatus.Paid.Id); - var order = await _orderRepository.GetAsync(orderStatusChangedToPaidDomainEvent.OrderId); + var order = await _orderRepository.GetAsync(domainEvent.OrderId); var buyer = await _buyerRepository.FindByIdAsync(order.GetBuyerId.Value.ToString()); - var orderStockList = orderStatusChangedToPaidDomainEvent.OrderItems + var orderStockList = domainEvent.OrderItems .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); - var orderStatusChangedToPaidIntegrationEvent = new OrderStatusChangedToPaidIntegrationEvent( - orderStatusChangedToPaidDomainEvent.OrderId, + var integrationEvent = new OrderStatusChangedToPaidIntegrationEvent( + domainEvent.OrderId, order.OrderStatus.Name, buyer.Name, orderStockList); - await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedToPaidIntegrationEvent); + await _orderingIntegrationEventService.AddAndSaveEventAsync(integrationEvent); } } diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToStockConfirmedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToStockConfirmedDomainEventHandler.cs index 00548c802..26aae4736 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToStockConfirmedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStatusChangedToStockConfirmedDomainEventHandler.cs @@ -5,13 +5,13 @@ public class OrderStatusChangedToStockConfirmedDomainEventHandler { private readonly IOrderRepository _orderRepository; private readonly IBuyerRepository _buyerRepository; - private readonly ILoggerFactory _logger; + private readonly ILogger _logger; private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; public OrderStatusChangedToStockConfirmedDomainEventHandler( IOrderRepository orderRepository, IBuyerRepository buyerRepository, - ILoggerFactory logger, + ILogger logger, IOrderingIntegrationEventService orderingIntegrationEventService) { _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); @@ -20,16 +20,14 @@ public class OrderStatusChangedToStockConfirmedDomainEventHandler _orderingIntegrationEventService = orderingIntegrationEventService; } - public async Task Handle(OrderStatusChangedToStockConfirmedDomainEvent orderStatusChangedToStockConfirmedDomainEvent, CancellationToken cancellationToken) + public async Task Handle(OrderStatusChangedToStockConfirmedDomainEvent domainEvent, CancellationToken cancellationToken) { - _logger.CreateLogger() - .LogTrace("Order with Id: {OrderId} has been successfully updated to status {Status} ({Id})", - orderStatusChangedToStockConfirmedDomainEvent.OrderId, nameof(OrderStatus.StockConfirmed), OrderStatus.StockConfirmed.Id); + OrderingApiTrace.LogOrderStatusUpdated(_logger, domainEvent.OrderId, nameof(OrderStatus.StockConfirmed), OrderStatus.StockConfirmed.Id); - var order = await _orderRepository.GetAsync(orderStatusChangedToStockConfirmedDomainEvent.OrderId); + var order = await _orderRepository.GetAsync(domainEvent.OrderId); var buyer = await _buyerRepository.FindByIdAsync(order.GetBuyerId.Value.ToString()); - var orderStatusChangedToStockConfirmedIntegrationEvent = new OrderStatusChangedToStockConfirmedIntegrationEvent(order.Id, order.OrderStatus.Name, buyer.Name); - await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedToStockConfirmedIntegrationEvent); + var integrationEvent = new OrderStatusChangedToStockConfirmedIntegrationEvent(order.Id, order.OrderStatus.Name, buyer.Name); + await _orderingIntegrationEventService.AddAndSaveEventAsync(integrationEvent); } } diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/SendEmailToCustomerWhenOrderStartedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/SendEmailToCustomerWhenOrderStartedDomainEventHandler.cs deleted file mode 100644 index 7350c16cd..000000000 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/SendEmailToCustomerWhenOrderStartedDomainEventHandler.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.DomainEventHandlers; - -public class SendEmailToCustomerWhenOrderStartedDomainEventHandler -//: IAsyncNotificationHandler -{ - public SendEmailToCustomerWhenOrderStartedDomainEventHandler() - { - - } - - //public async Task Handle(OrderStartedDomainEvent orderNotification) - //{ - // //TBD - Send email logic - //} -} diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs index 5202bf50f..5c14ac65c 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs @@ -1,13 +1,13 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.DomainEventHandlers; -public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler - : INotificationHandler +public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler : INotificationHandler { private readonly IOrderRepository _orderRepository; - private readonly ILoggerFactory _logger; + private readonly ILogger _logger; public UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler( - IOrderRepository orderRepository, ILoggerFactory logger) + IOrderRepository orderRepository, + ILogger logger) { _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); @@ -16,14 +16,11 @@ public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler // Domain Logic comment: // When the Buyer and Buyer's payment method have been created or verified that they existed, // then we can update the original Order with the BuyerId and PaymentId (foreign keys) - public async Task Handle(BuyerAndPaymentMethodVerifiedDomainEvent buyerPaymentMethodVerifiedEvent, CancellationToken cancellationToken) + public async Task Handle(BuyerAndPaymentMethodVerifiedDomainEvent domainEvent, CancellationToken cancellationToken) { - var orderToUpdate = await _orderRepository.GetAsync(buyerPaymentMethodVerifiedEvent.OrderId); - orderToUpdate.SetBuyerId(buyerPaymentMethodVerifiedEvent.Buyer.Id); - orderToUpdate.SetPaymentId(buyerPaymentMethodVerifiedEvent.Payment.Id); - - _logger.CreateLogger() - .LogTrace("Order with Id: {OrderId} has been successfully updated with a payment method {PaymentMethod} ({Id})", - buyerPaymentMethodVerifiedEvent.OrderId, nameof(buyerPaymentMethodVerifiedEvent.Payment), buyerPaymentMethodVerifiedEvent.Payment.Id); + var orderToUpdate = await _orderRepository.GetAsync(domainEvent.OrderId); + orderToUpdate.SetBuyerId(domainEvent.Buyer.Id); + orderToUpdate.SetPaymentId(domainEvent.Payment.Id); + OrderingApiTrace.LogOrderPaymentMethodUpdated(_logger, domainEvent.OrderId, nameof(domainEvent.Payment), domainEvent.Payment.Id); } } diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler.cs index 9940c5114..a613e3794 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler.cs @@ -3,53 +3,48 @@ public class ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler : INotificationHandler { - private readonly ILoggerFactory _logger; + private readonly ILogger _logger; private readonly IBuyerRepository _buyerRepository; - private readonly IIdentityService _identityService; private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; public ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler( - ILoggerFactory logger, + ILogger logger, IBuyerRepository buyerRepository, - IIdentityService identityService, IOrderingIntegrationEventService orderingIntegrationEventService) { _buyerRepository = buyerRepository ?? throw new ArgumentNullException(nameof(buyerRepository)); - _identityService = identityService ?? throw new ArgumentNullException(nameof(identityService)); _orderingIntegrationEventService = orderingIntegrationEventService ?? throw new ArgumentNullException(nameof(orderingIntegrationEventService)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public async Task Handle(OrderStartedDomainEvent orderStartedEvent, CancellationToken cancellationToken) + public async Task Handle(OrderStartedDomainEvent domainEvent, CancellationToken cancellationToken) { - var cardTypeId = orderStartedEvent.CardTypeId != 0 ? orderStartedEvent.CardTypeId : 1; - var buyer = await _buyerRepository.FindAsync(orderStartedEvent.UserId); - var buyerOriginallyExisted = buyer == null ? false : true; + var cardTypeId = domainEvent.CardTypeId != 0 ? domainEvent.CardTypeId : 1; + var buyer = await _buyerRepository.FindAsync(domainEvent.UserId); + var buyerExisted = buyer is not null; - if (!buyerOriginallyExisted) + if (!buyerExisted) { - buyer = new Buyer(orderStartedEvent.UserId, orderStartedEvent.UserName); + buyer = new Buyer(domainEvent.UserId, domainEvent.UserName); } buyer.VerifyOrAddPaymentMethod(cardTypeId, $"Payment Method on {DateTime.UtcNow}", - orderStartedEvent.CardNumber, - orderStartedEvent.CardSecurityNumber, - orderStartedEvent.CardHolderName, - orderStartedEvent.CardExpiration, - orderStartedEvent.Order.Id); + domainEvent.CardNumber, + domainEvent.CardSecurityNumber, + domainEvent.CardHolderName, + domainEvent.CardExpiration, + domainEvent.Order.Id); - var buyerUpdated = buyerOriginallyExisted ? + var buyerUpdated = buyerExisted ? _buyerRepository.Update(buyer) : _buyerRepository.Add(buyer); await _buyerRepository.UnitOfWork .SaveEntitiesAsync(cancellationToken); - var orderStatusChangedToSubmittedIntegrationEvent = new OrderStatusChangedToSubmittedIntegrationEvent(orderStartedEvent.Order.Id, orderStartedEvent.Order.OrderStatus.Name, buyer.Name); - await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedToSubmittedIntegrationEvent); - _logger.CreateLogger() - .LogTrace("Buyer {BuyerId} and related payment method were validated or updated for orderId: {OrderId}.", - buyerUpdated.Id, orderStartedEvent.Order.Id); + var integrationEvent = new OrderStatusChangedToSubmittedIntegrationEvent(domainEvent.Order.Id, domainEvent.Order.OrderStatus.Name, buyer.Name); + await _orderingIntegrationEventService.AddAndSaveEventAsync(integrationEvent); + OrderingApiTrace.LogOrderBuyerAndPaymentValidatedOrUpdated(_logger, buyerUpdated.Id, domainEvent.Order.Id); } } diff --git a/src/Services/Ordering/Ordering.API/Extensions/OrderingApiTrace.cs b/src/Services/Ordering/Ordering.API/Extensions/OrderingApiTrace.cs new file mode 100644 index 000000000..dfd2a78e5 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Extensions/OrderingApiTrace.cs @@ -0,0 +1,13 @@ +namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.DomainEventHandlers; + +internal static partial class OrderingApiTrace +{ + [LoggerMessage(EventId = 1, EventName = "OrderStatusUpdated", Level = LogLevel.Trace, Message = "Order with Id: {OrderId} has been successfully updated to status {Status} ({Id})")] + public static partial void LogOrderStatusUpdated(ILogger logger, int orderId, string status, int id); + + [LoggerMessage(EventId = 2, EventName = "PaymentMethodUpdated", Level = LogLevel.Trace, Message = "Order with Id: {OrderId} has been successfully updated with a payment method {PaymentMethod} ({Id})")] + public static partial void LogOrderPaymentMethodUpdated(ILogger logger, int orderId, string paymentMethod, int id); + + [LoggerMessage(EventId = 3, EventName = "BuyerAndPaymentValidatedOrUpdated", Level = LogLevel.Trace, Message = "Buyer {BuyerId} and related payment method were validated or updated for order Id: {OrderId}.")] + public static partial void LogOrderBuyerAndPaymentValidatedOrUpdated(ILogger logger, int buyerId, int orderId); +} diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs index e4e490488..2a95e98b9 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs @@ -1,7 +1,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; -public class PaymentMethod - : Entity +public class PaymentMethod : Entity { private string _alias; private string _cardNumber; @@ -12,12 +11,10 @@ public class PaymentMethod private int _cardTypeId; public CardType CardType { get; private set; } - protected PaymentMethod() { } public PaymentMethod(int cardTypeId, string alias, string cardNumber, string securityNumber, string cardHolderName, DateTime expiration) { - _cardNumber = !string.IsNullOrWhiteSpace(cardNumber) ? cardNumber : throw new OrderingDomainException(nameof(cardNumber)); _securityNumber = !string.IsNullOrWhiteSpace(securityNumber) ? securityNumber : throw new OrderingDomainException(nameof(securityNumber)); _cardHolderName = !string.IsNullOrWhiteSpace(cardHolderName) ? cardHolderName : throw new OrderingDomainException(nameof(cardHolderName)); diff --git a/src/Services/Webhooks/Webhooks.API/Startup.cs b/src/Services/Webhooks/Webhooks.API/Startup.cs index 32153a5cf..c79ea4b2e 100644 --- a/src/Services/Webhooks/Webhooks.API/Startup.cs +++ b/src/Services/Webhooks/Webhooks.API/Startup.cs @@ -8,7 +8,6 @@ public class Startup Configuration = configuration; } - public IServiceProvider ConfigureServices(IServiceCollection services) { services @@ -29,13 +28,13 @@ public class Startup return services.BuildServiceProvider(); } - public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) + public void Configure(IApplicationBuilder app, ILogger logger) { var pathBase = Configuration["PATH_BASE"]; if (!string.IsNullOrEmpty(pathBase)) { - loggerFactory.CreateLogger("init").LogDebug("Using PATH BASE '{PathBase}'", pathBase); + logger.LogDebug("Using PATH BASE '{PathBase}'", pathBase); app.UsePathBase(pathBase); } diff --git a/src/Web/WebSPA/Program.cs b/src/Web/WebSPA/Program.cs index 145151df7..82cf6feed 100644 --- a/src/Web/WebSPA/Program.cs +++ b/src/Web/WebSPA/Program.cs @@ -61,14 +61,13 @@ app.Use(next => context => }); // Seed Data -var loggerFactory = app.Services.GetRequiredService(); -WebContextSeed.Seed(app, app.Environment, loggerFactory); +WebContextSeed.Seed(app, app.Environment, app.Services.GetRequiredService>()); var pathBase = app.Configuration["PATH_BASE"]; if (!string.IsNullOrEmpty(pathBase)) { - loggerFactory.CreateLogger().LogDebug("Using PATH_BASE '{PathBase}'", pathBase); + app.Services.GetRequiredService>().LogDebug("Using PATH_BASE '{PathBase}'", pathBase); app.UsePathBase(pathBase); } diff --git a/src/Web/WebSPA/Server/Infrastructure/WebContextSeed.cs b/src/Web/WebSPA/Server/Infrastructure/WebContextSeed.cs index 483d3f9ce..1dc07b4f6 100644 --- a/src/Web/WebSPA/Server/Infrastructure/WebContextSeed.cs +++ b/src/Web/WebSPA/Server/Infrastructure/WebContextSeed.cs @@ -4,10 +4,8 @@ using Microsoft.Extensions.Logging; public class WebContextSeed { - public static void Seed(IApplicationBuilder applicationBuilder, IWebHostEnvironment env, ILoggerFactory loggerFactory) + public static void Seed(IApplicationBuilder applicationBuilder, IWebHostEnvironment env, ILogger logger) { - var log = loggerFactory.CreateLogger(); - var settings = applicationBuilder .ApplicationServices.GetRequiredService>().Value; @@ -17,18 +15,18 @@ public class WebContextSeed if (useCustomizationData) { - GetPreconfiguredImages(contentRootPath, webroot, log); + GetPreconfiguredImages(contentRootPath, webroot, logger); } } - static void GetPreconfiguredImages(string contentRootPath, string webroot, ILogger log) + static void GetPreconfiguredImages(string contentRootPath, string webroot, ILogger logger) { try { string imagesZipFile = Path.Combine(contentRootPath, "Setup", "images.zip"); if (!File.Exists(imagesZipFile)) { - log.LogError("Zip file '{ZipFileName}' does not exists.", imagesZipFile); + logger.LogError("Zip file '{ZipFileName}' does not exists.", imagesZipFile); return; } @@ -53,13 +51,13 @@ public class WebContextSeed } else { - log.LogWarning("Skipped file '{FileName}' in zipfile '{ZipFileName}'", entry.Name, imagesZipFile); + logger.LogWarning("Skipped file '{FileName}' in zipfile '{ZipFileName}'", entry.Name, imagesZipFile); } } } catch (Exception ex) { - log.LogError(ex, "ERROR in GetPreconfiguredImages: {Message}", ex.Message); + logger.LogError(ex, "ERROR in GetPreconfiguredImages: {Message}", ex.Message); } } }