Serilog configuration is only partially respected

Viewed 222

I use Serilog.AspNetCore 3.2.0 and Serilog.Sinks.File 4.2.0 in an ASP.NET Core 3.1 project and I have the following configuration in my appsettings.json:

"Serilog": {
    "WriteTo": [
      {
        "Name": "File",
        "MinimumLevel": "Verbose",
        "Enrich": "FromLogContext",
        "Args": {
          "path": "Server1_.log",
          "rollingInterval": "Day",
          "buffered": "false",
          "restrictedToMinimumLevel": "Verbose"
        }
      }
    ]
  }

I configure my services with the following:

.UseSerilog(
(hostingContext, loggerConfiguration) 
   => loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration)
)

When I change the output file name, it changes accordingly.

I use the folowing code for test:

Log.Information("Starting...");
Log.Debug("Starting...");
Log.Warning("Starting...");

This is the output:

2020-06-03 12:05:23.372 +02:00 [INF] Starting... 
2020-06-03 12:05:23.372 +02:00 [WRN] Starting...

Debug and Verbose levels are ignored.

How can I add lower levels to the log?

1 Answers

Your JSON schema isn't quite right; MinimumLevel and Enrich are top-level:

  "Serilog": {
    "MinimumLevel": "Verbose",
    "Enrich": ["FromLogContext"],
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "Server1_.log",
          "rollingInterval": "Day",
          "buffered": "false",
          "restrictedToMinimumLevel": "Verbose"
        }
      }

Further examples in the README.

Related