Skip to content

Commit f3cd373

Browse files
committed
Add logging to CreateOrderCommandHandler
1 parent 423066a commit f3cd373

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using MediatR;
77
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
88
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
9+
using Microsoft.Extensions.Logging;
910
using System;
1011
using System.Threading;
1112
using System.Threading.Tasks;
@@ -18,17 +19,20 @@ public class CreateOrderCommandHandler
1819
private readonly IIdentityService _identityService;
1920
private readonly IMediator _mediator;
2021
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
22+
private readonly ILogger<CreateOrderCommandHandler> _logger;
2123

2224
// Using DI to inject infrastructure persistence Repositories
2325
public CreateOrderCommandHandler(IMediator mediator,
2426
IOrderingIntegrationEventService orderingIntegrationEventService,
2527
IOrderRepository orderRepository,
26-
IIdentityService identityService)
28+
IIdentityService identityService,
29+
ILogger<CreateOrderCommandHandler> logger)
2730
{
2831
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
2932
_identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
3033
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
3134
_orderingIntegrationEventService = orderingIntegrationEventService ?? throw new ArgumentNullException(nameof(orderingIntegrationEventService));
35+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
3236
}
3337

3438
public async Task<bool> Handle(CreateOrderCommand message, CancellationToken cancellationToken)
@@ -49,6 +53,8 @@ public async Task<bool> Handle(CreateOrderCommand message, CancellationToken can
4953
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
5054
}
5155

56+
_logger.LogInformation("----- Creating Order - Order: {@Order}", order);
57+
5258
_orderRepository.Add(order);
5359

5460
return await _orderRepository.UnitOfWork

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
77
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Utilities;
88
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
9+
using Microsoft.Extensions.Logging;
910
using System;
1011
using System.Data.Common;
1112
using System.Diagnostics;
@@ -21,24 +22,30 @@ public class OrderingIntegrationEventService : IOrderingIntegrationEventService
2122
private readonly OrderingContext _orderingContext;
2223
private readonly IntegrationEventLogContext _eventLogContext;
2324
private readonly IIntegrationEventLogService _eventLogService;
25+
private readonly ILogger<OrderingIntegrationEventService> _logger;
2426

2527
public OrderingIntegrationEventService(IEventBus eventBus,
2628
OrderingContext orderingContext,
2729
IntegrationEventLogContext eventLogContext,
28-
Func<DbConnection, IIntegrationEventLogService> integrationEventLogServiceFactory)
30+
Func<DbConnection, IIntegrationEventLogService> integrationEventLogServiceFactory,
31+
ILogger<OrderingIntegrationEventService> logger)
2932
{
3033
_orderingContext = orderingContext ?? throw new ArgumentNullException(nameof(orderingContext));
3134
_eventLogContext = eventLogContext ?? throw new ArgumentNullException(nameof(eventLogContext));
3235
_integrationEventLogServiceFactory = integrationEventLogServiceFactory ?? throw new ArgumentNullException(nameof(integrationEventLogServiceFactory));
3336
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
3437
_eventLogService = _integrationEventLogServiceFactory(_orderingContext.Database.GetDbConnection());
38+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
3539
}
3640

3741
public async Task PublishEventsThroughEventBusAsync()
3842
{
3943
var pendindLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync();
44+
4045
foreach (var logEvt in pendindLogEvents)
4146
{
47+
_logger.LogInformation("----- Publishing integration event {IntegrationEventId} ({@IntegrationEvent})", logEvt.EventId, logEvt);
48+
4249
try
4350
{
4451
await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId);
@@ -54,6 +61,8 @@ public async Task PublishEventsThroughEventBusAsync()
5461

5562
public async Task AddAndSaveEventAsync(IntegrationEvent evt)
5663
{
64+
_logger.LogInformation("----- Saving integration event {IntegrationEventId} to repository ({@IntegrationEvent})", evt.Id, evt);
65+
5766
await _eventLogService.SaveEventAsync(evt, _orderingContext.GetCurrentTransaction.GetDbTransaction());
5867
}
5968
}

src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Ordering.API.Application.Behaviors;
88
using Ordering.API.Application.DomainEventHandlers.OrderStartedEvent;
99
using Ordering.API.Application.Validations;
10-
using Ordering.API.Infrastructure.Behaviors;
1110

1211
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.AutofacModules
1312
{

0 commit comments

Comments
 (0)