stevejgordon / CorrelationId

An ASP.NET Core middleware component which synchronises a correlation ID for cross API request logging.
MIT License
558 stars 106 forks source link

Disable or change log level of: "No correlation ID was found in the request headers" #103

Open Giovyx opened 3 years ago

Giovyx commented 3 years ago

Hello, I would like to disable or better change the log level of the _missingCorrelationIdHeader log to Debug rather than Information, since not are requests are required to have such header in my application.

INFO - CorrelationId.CorrelationIdMiddleware: "No correlation ID was found in the request headers"

Is there a way to achieve such thing? Or maybe consider adding it if not?

Regards

nj-git commented 3 years ago

I disabled it for Serilog using following command

public static Logger CreateLogger()
    {
        var configuration = LoadAppConfiguration();
        return new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .MinimumLevel.Override("CorrelationId", LogEventLevel.Error)
            .CreateLogger();
    }
freerider75 commented 2 years ago

Although I think that it would be handy to have a configuration option to either suppress this message or to choose which loglevel to use for it, I managed to mute it with a log filter in the ConfigureLogging call:

.ConfigureLogging(builder =>
    {
        // ... other configs ...  
        builder.AddFilter((category, level) =>
            !(category.Contains(nameof(CorrelationIdMiddleware), StringComparison.InvariantCultureIgnoreCase) && level < LogLevel.Error));
    }

With nameof I try to prevent regressions in case the source code changed (I know, it's weak...) and with the (adjustable) level filter I'm not excluding important logs.

cheers