Search code examples
c#.netasp.net-coremediatrrider

MediatR - IRequestPreProcessor - Incorrect type is used for contextual logging


I'm using MediatR's IRequestPreProcessor to log the requests. However, I'm getting the following warning at ILogger<TRequest> logger:

Incorrect type is used for contextual logging

How do I fix it?

public sealed class LoggingBehaviour<TRequest> : IRequestPreProcessor<TRequest> where TRequest : notnull
{
    private readonly ILogger<TRequest> _logger;

    public LoggingBehaviour(ILogger<TRequest> logger)
    {
        _logger = logger;
    }

    public async Task Process(TRequest request, CancellationToken cancellationToken)
    {
        var requestName = typeof(TRequest).Name;
        _logger.LogInformation("Request: {Name} {@Request}", requestName, request);
    }
}

Solution

  • Use the full class type: ILogger<LoggingBehaviour<TRequest>>

    Source: https://github.com/olsh/resharper-structured-logging/blob/master/rules/ContextualLoggerProblem.md