3

I had always imagined that

  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },

would set my project code's log level to Debug, and make Microsoft.* namespaces log at Information level or higher. It seems not. With this config, AspNetCore infrastructure logs at Debug level.

How do I target Microsoft.* to not use the Default level?

I'm using Serilog, but the injected Loggers are all Microsoft.Extensions.Logging.ILogger so I expected Microsoft.Extensions.Logging config to kick in.

Is this a mistaken assumption?

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61

1 Answers1

2

Yes, the assumption that Serilog will respect the Logging.LogLevel section when used with Extensions.Logging is wrong. It doesn't.

To read serilog levels from config, some options are:

var defaultLogLevel = configuration.GetLogLevel("Default");
var aspNetCoreLogLevel = configuration.GetLogLevel("Microsoft.AspNetCore");
var microsoftLogLevel = configuration.GetLogLevel("Microsoft");
var logger = new LoggerConfiguration()
                .MinimumLevel.Is(defaultLogLevel)
                .MinimumLevel.Override("Microsoft.AspNetCore", aspNetCoreLogLevel)
                .MinimumLevel.Override("Microsoft", microsoftLogLevel)
                // ... etc ...

// ...

static LogEventLevel GetLogLevel(this IConfiguration configuration, string @namespace, string fallbackLevel = "Information")
{
        return Enum.Parse<LogEventLevel>(configuration["Logging:LogLevel:" + @namespace] ?? fallbackLevel);
}

but the in-config approach for minimum levels has the advantage that you can make use of reloadOnChange

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61