Serilog Condition writing to file based on global minimum level

156 Views Asked by At

I'm trying to log my events in two different files based on the global minimum level set in the logger:

  • >= Debug to a debug file
  • >= Information to another file.

I control the minimum level through Dynamic Level and some logic in my app. In my json config file, I can use the "restrictedToMinimumLevel" in each sink to set the debug events to a specific file (see example below) But Info events will be copy in the Debug file even if the minimum level has been set to Information globally because the sink is set to minimum debug.

What I'm looking for is to not write anything to the debug sink if the minimum global level is being set to Information (MinimumLevel.ControlledBy()) If I don't choose to log debug, I don't want a debug file to be created basically... Is it possible to set up such filters/logic in the json file ?

Thank you for your help

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.File" ],
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "Logs/EventsLog.txt",
          "rollingInterval": "Day",
          "retainedFileCountLimit": 31,
          "buffered": false,
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj} {NewLine}{Exception}",
          "restrictedToMinimumLevel": "Information"

        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "Logs/DebugLog.txt",
          "rollingInterval": "Day",
          "retainedFileCountLimit": 2,
          "buffered": false,
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj} {Properties} {ThreadId} {ThreadName}{NewLine}{Exception}",
          "restrictedToMinimumLevel": "Debug"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithThreadName" ]   
  }
}

0

There are 0 best solutions below