Why isn't Serilog writing Debug messages even when the level is set to Debug?

I think it would need to be this...

LogEventLevel level = LogEventLevel.Information;
#if DEBUG
            level = LogEventLevel.Debug;
#endif

        UsageLogger = new LoggerConfiguration()
    #if DEBUG
    .MinimumLevel.Debug()
    #endif
           .Enrich.With(new ThreadIdEnricher())
           .WriteTo.File("UsageLogging.txt", restrictedToMinimumLevel: level, outputTemplate: LogTemplate, rollingInterval: RollingInterval.Day)
           .Enrich.With(new ThreadIdEnricher())
           .WriteTo.Console(restrictedToMinimumLevel: level, outputTemplate: LogTemplate)
           .Enrich.With(new ThreadIdEnricher())
            .CreateLogger();

Mine is an asp.net core 2.0 project and reading the config from appsetting.Development.json file

In the Startup.cs file, first you need to create the logger as follows.

var seriLogger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .ReadFrom.Configuration(configuration)    
    .CreateLogger();

Here its important to note that the minimum level is set to Verbose. Note

.MinimumLevel.Verbose()

Next the appsettings.Developement.json would look like the following.

{
  "ConnectionStrings": {
  "HPlusSportsConnection": "Data Source=DESKTOP-Feast\\sqlexpress;Initial Catalog=H_Plus_Sports;Persist Security Info=True;User ID=fakeUserId;Password=fakePassword"
},
"Serilog": {
"WriteTo": [
  {
    "Name": "Seq",
    "Args": {
      "restrictedToMinimumLevel": "Debug",
      "serverUrl": "http://localhost:5341"
    }
  },
  {
    "Name": "File",
    "Args": {
      "restrictedToMinimumLevel": "Verbose",
      "path": "log.txt",
      "outputTemplate": "Will be logged {Timestamp:yyyy-MMM-dd HH:mm:ss}|{TenantName}|{RequestId}|{SourceContext}|{Level:u3}|{Message:lj}{NewLine}{Exception}",
      "rollingInterval": "Day"
    }
  }
]},}

So I have multiple sinks and each has its own level. The sink Seq has Debug, so debug and above will be logged to seq sink. And to the text file, the level is Verbose, to effectively everything will be logged.

Again to emphasize,

.MinimumLevel.Verbose()

is important here. If you omit or comment out that, then file as well as seq will have only logs from information and above, even though you configured them to verbose or debug. Thats because minimum level is by default "Information".

Tags:

C#

Wpf

Serilog