41

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.

jollyroger23
  • 665
  • 1
  • 6
  • 19
  • 2
    How is this done in app.config? – PHenry May 07 '19 at 19:29
  • For a .Net application? I haven't needed this level of detail/differentiating for .net, just .net core. This is all I added to the .Net config after adding serilog: – jollyroger23 May 15 '19 at 15:34
  • 4
    I was able to get this to work in the app.config. The trick is to make sure serilog is actually paying attention with this line. Then for the logging to the file I used and then use this to log to the console Sorry for the formatting, I'm not sure how to do line feed in the comment. – PHenry May 17 '19 at 13:54
  • Thank you @PHenry for fishing out the app/web config setting to set the minimum level to debugging. I kept trying things like `serilog:MinimumLevel:Debug` to no avail – billinkc May 27 '20 at 17:34

3 Answers3

44

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"
      }
    ]
  },
Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
10

Working from the answers above, this is how I set it up in code as opposed to config

LoggerConfiguration GetConfig()
                => new LoggerConfiguration()
                    .WriteTo.Seq(serverUrl: "http://localhost:5341", restrictedToMinimumLevel: LogEventLevel.Debug)
                    .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Error);
            var logger = GetConfig().CreateLogger();
            builder.RegisterInstance(logger).As<ILogger>();

After I did this, it worked for log level Information and above. Debug didn't work. This post explained why.

I had to set a 'global' minimum level like this.

LoggerConfiguration GetConfig()
            => new LoggerConfiguration()
                .WriteTo.Seq(serverUrl: "http://localhost:5341", restrictedToMinimumLevel: LogEventLevel.Debug)
                .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Error)
                .MinimumLevel.Verbose();
Damien Sawyer
  • 5,323
  • 3
  • 44
  • 56
7

In your configuration you have one Serilog logger, but you have 2 sinks. One of your sinks is RollingFile and the other is Console.

You can override (but only raise) the minimum logging level per sink, The argument is called restrictedToMinimumLevel.

Since you want to raise the minimum logging level from your logger's default Debug to Warning in your file sink, in your appsettings.json file, it would look like this:

"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"
      }
    ]
  },
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76