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