Search code examples
c#.netloggingserilog

Show only debug level logs in Serilog


I want to show only debug level of logs, but none of the other levels, below or above the debug level. Also I want to do that in the configuration in appsettings.json, not in the startup class. Here is my appsettings.json file

 "Serilog": {
"MinimumLevel": {
  "Default": "Debug",
  "Override": {
    "System": "Debug",
    "Microsoft": "Debug"
  }
},
"WriteTo": [
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://localhost:5341/",
      "compact": true
    }
  },
  {
    "Name": "Console"
  },
  {
    "Name": "File",
    "Args": {
      "path": "D:\\dev.SmartCity.Peafowls.Logs\\PeafowlsLog_.txt",
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
      "rollingInterval": "Day",
      "rollOnFileSizeLimit": true,
      "fileSizeLimitBytes": 4194304
    }
  }
],
"Enrich": [ "FromLogContext" ] }

Solution

  • Check this answer. You can filter messages by level.

    Like this example:

    {
      "Serilog": {
        "Using": ["Serilog.Settings.Configuration"],
        "Filter": [
          {
            "Name": "ByExcluding",
            "Args": {
              "expression": "EndsWith(RequestPath, '/SomeEndpoint')"
            }
          }
        ]
    

    from Github source code.