Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

serilog

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();
        }
like image 854
shawn1874 Avatar asked Jan 10 '18 21:01

shawn1874


People also ask

What is debug mode in logger?

debug . This combination of a configurable logging level and logging statements within your program allow you full control over how your application will log its activity.

How do you debug logs?

To view a debug log, from Setup, enter Debug Logs in the Quick Find box, then select Debug Logs. Then click View next to the debug log that you want to examine. Click Download to download the log as an XML file. Debug logs have the following limits.

What is extra debug logging?

Debug logs are system-generated logs that are sent to your Dashboard along with every new conversation. They only appear if your developers have configured them in the SDK for a given game/app version. When configured, they appear under the metadata tab in the Issue details pane.


2 Answers

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();
like image 54
pmcilreavy Avatar answered Sep 16 '22 11:09

pmcilreavy


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

like image 24
VivekDev Avatar answered Sep 16 '22 11:09

VivekDev