| | | 1 | | using System.Diagnostics; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using RoleRadar.Application.Abstractions; |
| | | 4 | | |
| | | 5 | | namespace RoleRadar.Application.Decorators; |
| | | 6 | | |
| | | 7 | | public sealed class LoggingCommandDecorator<TCommand, TResponse> : ICommandHandler<TCommand, TResponse> |
| | | 8 | | where TCommand : ICommand<TResponse> |
| | | 9 | | { |
| | | 10 | | private static readonly Action<ILogger, string, Exception?> LogStarting = |
| | | 11 | | LoggerMessage.Define<string>( |
| | | 12 | | LogLevel.Information, |
| | | 13 | | new EventId(1001, "CommandStarting"), |
| | | 14 | | "Executing command {CommandName}"); |
| | | 15 | | |
| | | 16 | | private static readonly Action<ILogger, string, double, Exception?> LogSucceeded = |
| | | 17 | | LoggerMessage.Define<string, double>( |
| | | 18 | | LogLevel.Information, |
| | | 19 | | new EventId(1002, "CommandSucceeded"), |
| | | 20 | | "Command {CommandName} succeeded in {ElapsedMs:F2} ms"); |
| | | 21 | | |
| | | 22 | | private static readonly Action<ILogger, string, ErrorCode, double, Exception?> LogFailed = |
| | | 23 | | LoggerMessage.Define<string, ErrorCode, double>( |
| | | 24 | | LogLevel.Warning, |
| | | 25 | | new EventId(1003, "CommandFailed"), |
| | | 26 | | "Command {CommandName} returned failure {ErrorCode} in {ElapsedMs:F2} ms"); |
| | | 27 | | |
| | | 28 | | private readonly ICommandHandler<TCommand, TResponse> _inner; |
| | | 29 | | private readonly ILogger<LoggingCommandDecorator<TCommand, TResponse>> _logger; |
| | | 30 | | |
| | | 31 | | public LoggingCommandDecorator( |
| | | 32 | | ICommandHandler<TCommand, TResponse> inner, |
| | | 33 | | ILogger<LoggingCommandDecorator<TCommand, TResponse>> logger) |
| | | 34 | | { |
| | | 35 | | ArgumentNullException.ThrowIfNull(inner); |
| | | 36 | | ArgumentNullException.ThrowIfNull(logger); |
| | | 37 | | |
| | | 38 | | _inner = inner; |
| | | 39 | | _logger = logger; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<Result<TResponse>> HandleAsync( |
| | | 43 | | TCommand command, |
| | | 44 | | CancellationToken cancellationToken = default) |
| | | 45 | | { |
| | | 46 | | var commandName = typeof(TCommand).Name; |
| | | 47 | | LogStarting(_logger, commandName, null); |
| | | 48 | | var sw = Stopwatch.StartNew(); |
| | | 49 | | |
| | | 50 | | var result = await _inner.HandleAsync(command, cancellationToken); |
| | | 51 | | sw.Stop(); |
| | | 52 | | |
| | | 53 | | if (result.IsSuccess) |
| | | 54 | | { |
| | | 55 | | LogSucceeded(_logger, commandName, sw.Elapsed.TotalMilliseconds, null); |
| | | 56 | | } |
| | | 57 | | else |
| | | 58 | | { |
| | | 59 | | LogFailed(_logger, commandName, result.Error?.Code ?? ErrorCode.ValidationError, sw.Elapsed.TotalMillisecond |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | return result; |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public sealed class LoggingQueryDecorator<TQuery, TResponse> : IQueryHandler<TQuery, TResponse> |
| | | 67 | | where TQuery : IQuery<TResponse> |
| | | 68 | | { |
| | 224 | 69 | | private static readonly Action<ILogger, string, Exception?> LogStarting = |
| | 224 | 70 | | LoggerMessage.Define<string>( |
| | 224 | 71 | | LogLevel.Debug, |
| | 224 | 72 | | new EventId(2001, "QueryStarting"), |
| | 224 | 73 | | "Executing query {QueryName}"); |
| | | 74 | | |
| | 224 | 75 | | private static readonly Action<ILogger, string, double, Exception?> LogSucceeded = |
| | 224 | 76 | | LoggerMessage.Define<string, double>( |
| | 224 | 77 | | LogLevel.Debug, |
| | 224 | 78 | | new EventId(2002, "QuerySucceeded"), |
| | 224 | 79 | | "Query {QueryName} succeeded in {ElapsedMs:F2} ms"); |
| | | 80 | | |
| | 224 | 81 | | private static readonly Action<ILogger, string, ErrorCode, double, Exception?> LogFailed = |
| | 224 | 82 | | LoggerMessage.Define<string, ErrorCode, double>( |
| | 224 | 83 | | LogLevel.Warning, |
| | 224 | 84 | | new EventId(2003, "QueryFailed"), |
| | 224 | 85 | | "Query {QueryName} returned failure {ErrorCode} in {ElapsedMs:F2} ms"); |
| | | 86 | | |
| | | 87 | | private readonly IQueryHandler<TQuery, TResponse> _inner; |
| | | 88 | | private readonly ILogger<LoggingQueryDecorator<TQuery, TResponse>> _logger; |
| | | 89 | | |
| | 690 | 90 | | public LoggingQueryDecorator( |
| | 690 | 91 | | IQueryHandler<TQuery, TResponse> inner, |
| | 690 | 92 | | ILogger<LoggingQueryDecorator<TQuery, TResponse>> logger) |
| | | 93 | | { |
| | 690 | 94 | | ArgumentNullException.ThrowIfNull(inner); |
| | 690 | 95 | | ArgumentNullException.ThrowIfNull(logger); |
| | | 96 | | |
| | 690 | 97 | | _inner = inner; |
| | 690 | 98 | | _logger = logger; |
| | 690 | 99 | | } |
| | | 100 | | |
| | | 101 | | public async Task<Result<TResponse>> HandleAsync( |
| | | 102 | | TQuery query, |
| | | 103 | | CancellationToken cancellationToken = default) |
| | | 104 | | { |
| | 622 | 105 | | var queryName = typeof(TQuery).Name; |
| | 622 | 106 | | LogStarting(_logger, queryName, null); |
| | 622 | 107 | | var sw = Stopwatch.StartNew(); |
| | | 108 | | |
| | 622 | 109 | | var result = await _inner.HandleAsync(query, cancellationToken); |
| | 622 | 110 | | sw.Stop(); |
| | | 111 | | |
| | 622 | 112 | | if (result.IsSuccess) |
| | | 113 | | { |
| | 564 | 114 | | LogSucceeded(_logger, queryName, sw.Elapsed.TotalMilliseconds, null); |
| | | 115 | | } |
| | | 116 | | else |
| | | 117 | | { |
| | 58 | 118 | | LogFailed(_logger, queryName, result.Error?.Code ?? ErrorCode.ValidationError, sw.Elapsed.TotalMilliseconds, |
| | | 119 | | } |
| | | 120 | | |
| | 622 | 121 | | return result; |
| | 622 | 122 | | } |
| | | 123 | | } |