25

I wrote the following line to create my logger in a C#/WPF application, but the Debug messages do not show up in the logs. What am I missing? I am using serilog.sinks.file version 4.0.0. The release build produces information level events, but the debug build does not produce debug messages. I have confirmed that the DEBUG symbol is defined, and I've debugged to confirm that the level is in fact set to debug.

LogEventLevel level = LogEventLevel.Information;
#if DEBUG
            level = LogEventLevel.Debug;
#endif
            UsageLogger = new LoggerConfiguration()
               .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();
        }
shawn1874
  • 1,288
  • 1
  • 10
  • 26

3 Answers3

29

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();
pmcilreavy
  • 3,076
  • 2
  • 28
  • 37
  • 1
    Thank you. I thought the restrictedToMinimumLevel on each would have been enough to do that but the other function call is necessary to set the overall level for the configuration. I misunderstood the documentation. – shawn1874 Jan 10 '18 at 23:14
  • 10
    `MinimumLevel.Is(level)` would do it, too; you don't need to specify `restrictedToMinimumLevel` as well, here - HTH :-) – Nicholas Blumhardt Jan 11 '18 at 03:24
  • Thank you. I was having this problem with a writer to MSSqlServer. The key in the answer is the ".MinimumLevel.Debug()" at the logger level in addition to the restrictedToMinimumLevel at the sink level. My sink had the restrictedToMinimumLevel set, but didn't have the ".MinimumLevel.Debug()" set at the logger level. As a result it wasn't working. Evidently that value is inherited by each sink writer? I found this documentation very useful: https://github.com/serilog/serilog/wiki/Configuration-Basics#minimum-level – Michael Russ Feb 06 '20 at 17:35
  • In my case it took me a while to realise after reading these answer. Add `MinimumLevel` !AFTER! WriteTo. Because that is what it applies to. When its before the logger Verbose only logs Verbose data but still on the default log level INFO! Ohh man spent way to long on that – Piotr Kula Dec 17 '21 at 20:49
13

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".

VivekDev
  • 20,868
  • 27
  • 132
  • 202
  • to make it even more configurable it's possible to add "MinimumLevel": "Verbose" right above the "WriteTo" row – dario Feb 14 '22 at 09:22
  • I don't think this is the proper solution. This just forces debug to always print even if i later (since i'm trying to control the filter via command line options) set the minimum filter level in the LoggerFilterOptions – arkon Aug 25 '22 at 22:29
1

Change Default from information to debug in appsettings like this:

"Logging": {
"MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
}
Timothy G.
  • 6,335
  • 7
  • 30
  • 46