< Summary

Information
Class: RoleRadar.Application.Decorators.LoggingQueryDecorator<T1, T2>
Assembly: RoleRadar.Application
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Decorators/LoggingDecorators.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 123
Line coverage: 100%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
HandleAsync()75%44100%

File(s)

/home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Decorators/LoggingDecorators.cs

#LineLine coverage
 1using System.Diagnostics;
 2using Microsoft.Extensions.Logging;
 3using RoleRadar.Application.Abstractions;
 4
 5namespace RoleRadar.Application.Decorators;
 6
 7public 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
 66public sealed class LoggingQueryDecorator<TQuery, TResponse> : IQueryHandler<TQuery, TResponse>
 67    where TQuery : IQuery<TResponse>
 68{
 22469    private static readonly Action<ILogger, string, Exception?> LogStarting =
 22470        LoggerMessage.Define<string>(
 22471            LogLevel.Debug,
 22472            new EventId(2001, "QueryStarting"),
 22473            "Executing query {QueryName}");
 74
 22475    private static readonly Action<ILogger, string, double, Exception?> LogSucceeded =
 22476        LoggerMessage.Define<string, double>(
 22477            LogLevel.Debug,
 22478            new EventId(2002, "QuerySucceeded"),
 22479            "Query {QueryName} succeeded in {ElapsedMs:F2} ms");
 80
 22481    private static readonly Action<ILogger, string, ErrorCode, double, Exception?> LogFailed =
 22482        LoggerMessage.Define<string, ErrorCode, double>(
 22483            LogLevel.Warning,
 22484            new EventId(2003, "QueryFailed"),
 22485            "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
 69090    public LoggingQueryDecorator(
 69091        IQueryHandler<TQuery, TResponse> inner,
 69092        ILogger<LoggingQueryDecorator<TQuery, TResponse>> logger)
 93    {
 69094        ArgumentNullException.ThrowIfNull(inner);
 69095        ArgumentNullException.ThrowIfNull(logger);
 96
 69097        _inner = inner;
 69098        _logger = logger;
 69099    }
 100
 101    public async Task<Result<TResponse>> HandleAsync(
 102        TQuery query,
 103        CancellationToken cancellationToken = default)
 104    {
 622105        var queryName = typeof(TQuery).Name;
 622106        LogStarting(_logger, queryName, null);
 622107        var sw = Stopwatch.StartNew();
 108
 622109        var result = await _inner.HandleAsync(query, cancellationToken);
 622110        sw.Stop();
 111
 622112        if (result.IsSuccess)
 113        {
 564114            LogSucceeded(_logger, queryName, sw.Elapsed.TotalMilliseconds, null);
 115        }
 116        else
 117        {
 58118            LogFailed(_logger, queryName, result.Error?.Code ?? ErrorCode.ValidationError, sw.Elapsed.TotalMilliseconds,
 119        }
 120
 622121        return result;
 622122    }
 123}