Different Minimum Level Logs Serilog

jollyroger23 picture jollyroger23 · Aug 27, 2018 · Viewed 20.9k times · Source

Is there a way to differentiate what level is logged between the different loggers for Serilog? I want to be able to log MinimumLevel Debug to the console output but only Warning and above to my file output. I am using ASP.NET Core 2.1 and this is what the appsetting.json currently looks like:

"Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "RollingFile",
        "IsJson": true,
        "Args": {
          "pathFormat": "C:\\Logs\\Log-{Hour}.json",
          "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
        }
      },
      {
        "Name": "Console"
      }
    ]
  },

Is it something like another parameter under "Args"? I've tried "minimumnLevel" in this location but it did not work.

Answer

Kirk Larkin picture Kirk Larkin · Aug 27, 2018

The setting you're looking for is restrictedToMinimumLevel. This GitHub issue shows some examples of this, but for your example, you just need to add restrictedToMinimumLevel to your Args for RollingFile:

"Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "RollingFile",
        "IsJson": true,
        "Args": {
          "pathFormat": "C:\\Logs\\Log-{Hour}.json",
          "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
          "restrictedToMinimumLevel": "Warning"
        }
      },
      {
        "Name": "Console"
      }
    ]
  },