< Summary

Information
Class: RoleRadar.Application.Decorators.LoggingCommandDecorator<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{
 21010    private static readonly Action<ILogger, string, Exception?> LogStarting =
 21011        LoggerMessage.Define<string>(
 21012            LogLevel.Information,
 21013            new EventId(1001, "CommandStarting"),
 21014            "Executing command {CommandName}");
 15
 21016    private static readonly Action<ILogger, string, double, Exception?> LogSucceeded =
 21017        LoggerMessage.Define<string, double>(
 21018            LogLevel.Information,
 21019            new EventId(1002, "CommandSucceeded"),
 21020            "Command {CommandName} succeeded in {ElapsedMs:F2} ms");
 21
 21022    private static readonly Action<ILogger, string, ErrorCode, double, Exception?> LogFailed =
 21023        LoggerMessage.Define<string, ErrorCode, double>(
 21024            LogLevel.Warning,
 21025            new EventId(1003, "CommandFailed"),
 21026            "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
 227631    public LoggingCommandDecorator(
 227632        ICommandHandler<TCommand, TResponse> inner,
 227633        ILogger<LoggingCommandDecorator<TCommand, TResponse>> logger)
 34    {
 227635        ArgumentNullException.ThrowIfNull(inner);
 227636        ArgumentNullException.ThrowIfNull(logger);
 37
 227638        _inner = inner;
 227639        _logger = logger;
 227640    }
 41
 42    public async Task<Result<TResponse>> HandleAsync(
 43        TCommand command,
 44        CancellationToken cancellationToken = default)
 45    {
 71446        var commandName = typeof(TCommand).Name;
 71447        LogStarting(_logger, commandName, null);
 71448        var sw = Stopwatch.StartNew();
 49
 71450        var result = await _inner.HandleAsync(command, cancellationToken);
 71251        sw.Stop();
 52
 71253        if (result.IsSuccess)
 54        {
 64655            LogSucceeded(_logger, commandName, sw.Elapsed.TotalMilliseconds, null);
 56        }
 57        else
 58        {
 6659            LogFailed(_logger, commandName, result.Error?.Code ?? ErrorCode.ValidationError, sw.Elapsed.TotalMillisecond
 60        }
 61
 71262        return result;
 71263    }
 64}
 65
 66public 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}