Skip to content

Commit ad7b332

Browse files
committed
Add log traces for commands
1 parent 12148c9 commit ad7b332

15 files changed

Lines changed: 130 additions & 15 deletions

src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class CreateOrderCommandHandler
2424
// Using DI to inject infrastructure persistence Repositories
2525
public CreateOrderCommandHandler(IMediator mediator,
2626
IOrderingIntegrationEventService orderingIntegrationEventService,
27-
IOrderRepository orderRepository,
27+
IOrderRepository orderRepository,
2828
IIdentityService identityService,
2929
ILogger<CreateOrderCommandHandler> logger)
3030
{
@@ -40,22 +40,22 @@ public async Task<bool> Handle(CreateOrderCommand message, CancellationToken can
4040
// Add Integration event to clean the basket
4141
var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(message.UserId);
4242
await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStartedIntegrationEvent);
43-
43+
4444
// Add/Update the Buyer AggregateRoot
4545
// DDD patterns comment: Add child entities and value-objects through the Order Aggregate-Root
4646
// methods and constructor so validations, invariants and business logic
4747
// make sure that consistency is preserved across the whole aggregate
4848
var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode);
4949
var order = new Order(message.UserId, message.UserName, address, message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration);
50-
50+
5151
foreach (var item in message.OrderItems)
5252
{
5353
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
5454
}
5555

5656
_logger.LogInformation("----- Creating Order - Order: {@Order}", order);
5757

58-
_orderRepository.Add(order);
58+
_orderRepository.Add(order);
5959

6060
return await _orderRepository.UnitOfWork
6161
.SaveEntitiesAsync();
@@ -66,7 +66,11 @@ public async Task<bool> Handle(CreateOrderCommand message, CancellationToken can
6666
// Use for Idempotency in Command process
6767
public class CreateOrderIdentifiedCommandHandler : IdentifiedCommandHandler<CreateOrderCommand, bool>
6868
{
69-
public CreateOrderIdentifiedCommandHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager)
69+
public CreateOrderIdentifiedCommandHandler(
70+
IMediator mediator,
71+
IRequestManager requestManager,
72+
ILogger<IdentifiedCommandHandler<CreateOrderCommand, bool>> logger)
73+
: base(mediator, requestManager, logger)
7074
{
7175
}
7276

src/Services/Ordering/Ordering.API/Application/Commands/SetAwaitingValidationOrderStatusCommandHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
33
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
44
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
5+
using Microsoft.Extensions.Logging;
56
using System.Threading;
67
using System.Threading.Tasks;
78

@@ -40,7 +41,11 @@ public async Task<bool> Handle(SetAwaitingValidationOrderStatusCommand command,
4041
// Use for Idempotency in Command process
4142
public class SetAwaitingValidationIdentifiedOrderStatusCommandHandler : IdentifiedCommandHandler<SetAwaitingValidationOrderStatusCommand, bool>
4243
{
43-
public SetAwaitingValidationIdentifiedOrderStatusCommandHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager)
44+
public SetAwaitingValidationIdentifiedOrderStatusCommandHandler(
45+
IMediator mediator,
46+
IRequestManager requestManager,
47+
ILogger<IdentifiedCommandHandler<SetAwaitingValidationOrderStatusCommand, bool>> logger)
48+
: base(mediator, requestManager, logger)
4449
{
4550
}
4651

src/Services/Ordering/Ordering.API/Application/Commands/SetPaidOrderStatusCommandHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
33
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
44
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
5+
using Microsoft.Extensions.Logging;
56
using System.Threading;
67
using System.Threading.Tasks;
78

@@ -43,7 +44,11 @@ public async Task<bool> Handle(SetPaidOrderStatusCommand command, CancellationTo
4344
// Use for Idempotency in Command process
4445
public class SetPaidIdentifiedOrderStatusCommandHandler : IdentifiedCommandHandler<SetPaidOrderStatusCommand, bool>
4546
{
46-
public SetPaidIdentifiedOrderStatusCommandHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager)
47+
public SetPaidIdentifiedOrderStatusCommandHandler(
48+
IMediator mediator,
49+
IRequestManager requestManager,
50+
ILogger<IdentifiedCommandHandler<SetPaidOrderStatusCommand, bool>> logger)
51+
: base(mediator, requestManager, logger)
4752
{
4853
}
4954

src/Services/Ordering/Ordering.API/Application/Commands/SetStockConfirmedOrderStatusCommandHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
33
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
44
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
5+
using Microsoft.Extensions.Logging;
56
using System.Threading;
67
using System.Threading.Tasks;
78

@@ -43,7 +44,11 @@ public async Task<bool> Handle(SetStockConfirmedOrderStatusCommand command, Canc
4344
// Use for Idempotency in Command process
4445
public class SetStockConfirmedOrderStatusIdenfifiedCommandHandler : IdentifiedCommandHandler<SetStockConfirmedOrderStatusCommand, bool>
4546
{
46-
public SetStockConfirmedOrderStatusIdenfifiedCommandHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager)
47+
public SetStockConfirmedOrderStatusIdenfifiedCommandHandler(
48+
IMediator mediator,
49+
IRequestManager requestManager,
50+
ILogger<IdentifiedCommandHandler<SetStockConfirmedOrderStatusCommand, bool>> logger)
51+
: base(mediator, requestManager, logger)
4752
{
4853
}
4954

src/Services/Ordering/Ordering.API/Application/Commands/SetStockRejectedOrderStatusCommandHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
33
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
44
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
5+
using Microsoft.Extensions.Logging;
56
using System.Threading;
67
using System.Threading.Tasks;
78

@@ -44,7 +45,11 @@ public async Task<bool> Handle(SetStockRejectedOrderStatusCommand command, Cance
4445
// Use for Idempotency in Command process
4546
public class SetStockRejectedOrderStatusIdenfifiedCommandHandler : IdentifiedCommandHandler<SetStockRejectedOrderStatusCommand, bool>
4647
{
47-
public SetStockRejectedOrderStatusIdenfifiedCommandHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager)
48+
public SetStockRejectedOrderStatusIdenfifiedCommandHandler(
49+
IMediator mediator,
50+
IRequestManager requestManager,
51+
ILogger<IdentifiedCommandHandler<SetStockRejectedOrderStatusCommand, bool>> logger)
52+
: base(mediator, requestManager, logger)
4853
{
4954
}
5055

src/Services/Ordering/Ordering.API/Application/Commands/ShipOrderCommandHandler.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
33
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
44
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
5+
using Microsoft.Extensions.Logging;
56
using System.Threading;
67
using System.Threading.Tasks;
78

89
namespace Ordering.API.Application.Commands
910
{
1011
// Regular CommandHandler
1112
public class ShipOrderCommandHandler : IRequestHandler<ShipOrderCommand, bool>
12-
{
13+
{
1314
private readonly IOrderRepository _orderRepository;
1415

1516
public ShipOrderCommandHandler(IOrderRepository orderRepository)
@@ -26,7 +27,7 @@ public ShipOrderCommandHandler(IOrderRepository orderRepository)
2627
public async Task<bool> Handle(ShipOrderCommand command, CancellationToken cancellationToken)
2728
{
2829
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
29-
if(orderToUpdate == null)
30+
if (orderToUpdate == null)
3031
{
3132
return false;
3233
}
@@ -40,7 +41,11 @@ public async Task<bool> Handle(ShipOrderCommand command, CancellationToken cance
4041
// Use for Idempotency in Command process
4142
public class ShipOrderIdentifiedCommandHandler : IdentifiedCommandHandler<ShipOrderCommand, bool>
4243
{
43-
public ShipOrderIdentifiedCommandHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager)
44+
public ShipOrderIdentifiedCommandHandler(
45+
IMediator mediator,
46+
IRequestManager requestManager,
47+
ILogger<IdentifiedCommandHandler<ShipOrderCommand, bool>> logger)
48+
: base(mediator, requestManager, logger)
4449
{
4550
}
4651

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/GracePeriodConfirmedIntegrationEventHandler.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.eShopOnContainers.Services.Ordering.API;
44
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
55
using Microsoft.Extensions.Logging;
6+
using Ordering.API.Application.Behaviors;
67
using Ordering.API.Application.Commands;
78
using Ordering.API.Application.IntegrationEvents.Events;
89
using Serilog.Context;
@@ -38,6 +39,14 @@ public async Task Handle(GracePeriodConfirmedIntegrationEvent @event)
3839
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
3940

4041
var command = new SetAwaitingValidationOrderStatusCommand(@event.OrderId);
42+
43+
_logger.LogInformation(
44+
"----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})",
45+
command.GetGenericTypeName(),
46+
nameof(command.OrderNumber),
47+
command.OrderNumber,
48+
command);
49+
4150
await _mediator.Send(command);
4251
}
4352
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.eShopOnContainers.Services.Ordering.API;
66
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
77
using Microsoft.Extensions.Logging;
8+
using Ordering.API.Application.Behaviors;
89
using Ordering.API.Application.Commands;
910
using Ordering.API.Application.IntegrationEvents.Events;
1011
using Serilog.Context;
@@ -32,6 +33,14 @@ public async Task Handle(OrderPaymentFailedIntegrationEvent @event)
3233
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
3334

3435
var command = new CancelOrderCommand(@event.OrderId);
36+
37+
_logger.LogInformation(
38+
"----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})",
39+
command.GetGenericTypeName(),
40+
nameof(command.OrderNumber),
41+
command.OrderNumber,
42+
command);
43+
3544
await _mediator.Send(command);
3645
}
3746
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.eShopOnContainers.Services.Ordering.API;
66
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
77
using Microsoft.Extensions.Logging;
8+
using Ordering.API.Application.Behaviors;
89
using Ordering.API.Application.Commands;
910
using Ordering.API.Application.IntegrationEvents.Events;
1011
using Serilog.Context;
@@ -32,6 +33,14 @@ public async Task Handle(OrderPaymentSuccededIntegrationEvent @event)
3233
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
3334

3435
var command = new SetPaidOrderStatusCommand(@event.OrderId);
36+
37+
_logger.LogInformation(
38+
"----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})",
39+
command.GetGenericTypeName(),
40+
nameof(command.OrderNumber),
41+
command.OrderNumber,
42+
command);
43+
3544
await _mediator.Send(command);
3645
}
3746
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.Extensions.Logging;
1111
using Serilog.Context;
1212
using Microsoft.eShopOnContainers.Services.Ordering.API;
13+
using Ordering.API.Application.Behaviors;
1314

1415
public class OrderStockConfirmedIntegrationEventHandler :
1516
IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>
@@ -32,6 +33,14 @@ public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
3233
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
3334

3435
var command = new SetStockConfirmedOrderStatusCommand(@event.OrderId);
36+
37+
_logger.LogInformation(
38+
"----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})",
39+
command.GetGenericTypeName(),
40+
nameof(command.OrderNumber),
41+
command.OrderNumber,
42+
command);
43+
3544
await _mediator.Send(command);
3645
}
3746
}

0 commit comments

Comments
 (0)