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

shawn1874 picture shawn1874 · Jan 10, 2018 · Viewed 9.7k times · Source

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();
        }

Answer

pmcilreavy picture pmcilreavy · Jan 10, 2018

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();