diff --git a/samples/Conveyor.Services.Orders/Commands/Handlers/CreateOrderHandler.cs b/samples/Conveyor.Services.Orders/Commands/Handlers/CreateOrderHandler.cs index 2ee72cb3..cb4edf15 100644 --- a/samples/Conveyor.Services.Orders/Commands/Handlers/CreateOrderHandler.cs +++ b/samples/Conveyor.Services.Orders/Commands/Handlers/CreateOrderHandler.cs @@ -34,7 +34,18 @@ public CreateOrderHandler(IMongoRepository repository, IBusPublishe _logger = logger; } - public async Task HandleAsync(CreateOrder command, CancellationToken cancellationToken = default) + public async Task HandleAsync(CreateOrder command, CancellationToken cancellationToken) + { + await HandleCreateOrder(command, cancellationToken); + } + + + public async Task HandleAsync(CreateOrder command) + { + await HandleCreateOrder(command, CancellationToken.None); + } + + private async Task HandleCreateOrder(CreateOrder command, CancellationToken cancellationToken) { var exists = await _repository.ExistsAsync(o => o.Id == command.OrderId); if (exists) diff --git a/src/Convey.CQRS.Commands/src/Convey.CQRS.Commands/ICommandHandler.cs b/src/Convey.CQRS.Commands/src/Convey.CQRS.Commands/ICommandHandler.cs index e026d7e1..0d342f6c 100644 --- a/src/Convey.CQRS.Commands/src/Convey.CQRS.Commands/ICommandHandler.cs +++ b/src/Convey.CQRS.Commands/src/Convey.CQRS.Commands/ICommandHandler.cs @@ -5,5 +5,6 @@ namespace Convey.CQRS.Commands; public interface ICommandHandler where TCommand : class, ICommand { - Task HandleAsync(TCommand command, CancellationToken cancellationToken = default); + Task HandleAsync(TCommand command, CancellationToken cancellationToken) => HandleAsync((command)); + Task HandleAsync(TCommand command); } \ No newline at end of file diff --git a/src/Convey.Logging.CQRS/src/Convey.Logging.CQRS/Decorators/CommandHandlerLoggingDecorator.cs b/src/Convey.Logging.CQRS/src/Convey.Logging.CQRS/Decorators/CommandHandlerLoggingDecorator.cs index 4acf0062..5618fc52 100644 --- a/src/Convey.Logging.CQRS/src/Convey.Logging.CQRS/Decorators/CommandHandlerLoggingDecorator.cs +++ b/src/Convey.Logging.CQRS/src/Convey.Logging.CQRS/Decorators/CommandHandlerLoggingDecorator.cs @@ -25,7 +25,7 @@ public CommandHandlerLoggingDecorator(ICommandHandler handler, _mapper = serviceProvider.GetService() ?? new EmptyMessageToLogTemplateMapper(); } - public async Task HandleAsync(TCommand command, CancellationToken cancellationToken = default) + public async Task HandleAsync(TCommand command, CancellationToken cancellationToken) { var template = _mapper.Map(command); @@ -50,6 +50,31 @@ public async Task HandleAsync(TCommand command, CancellationToken cancellationTo } } + public async Task HandleAsync(TCommand command) + { + var template = _mapper.Map(command); + + if (template is null) + { + await _handler.HandleAsync(command); + return; + } + + try + { + Log(command, template.Before); + await _handler.HandleAsync(command); + Log(command, template.After); + } + catch (Exception ex) + { + var exceptionTemplate = template.GetExceptionTemplate(ex); + + Log(command, exceptionTemplate, isError: true); + throw; + } + } + private void Log(TCommand command, string message, bool isError = false) { if (string.IsNullOrEmpty(message))