SidD
SidD

Reputation: 6377

Multiple logging files with specific log type in Serilog

I have implemented Serilog in .net core,I wanted to achieve multiple logging files for each log type. For example Error.log file will only contain error,Trace.log file will only contain Trace log, Warning.log will only contain warning messages and not error messages. I have used Serilog.Sinks.RollingFile and restrictedToMinimumLevel, i am able to generate multiple files but due to restrictedToMinimumLevel, it logs above defined level as well. Is there any option/way to set logOnlyLevel kind of thing. I can use filter but is there any better option.

"Serilog": {
"Using": ["Serilog.Sinks.RollingFile"],
"MinimumLevel": {
    "Default": "Verbose",
    "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
    }
},
"WriteTo": [{
        "Name": "RollingFile",
        "Args": {
            "pathFormat": "Logs/Information.txt",
            "rollingInterval": "Day",
            "fileSizeLimitBytes": "2000000",
            "retainedFileCountLimit": "10",
            "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}|{EventId}|{Message}|{Scope} {NewLine}",
            "restrictedToMinimumLevel": "Information"
        }
    }, {
        "Name": "RollingFile",
        "Args": {
            "pathFormat": "Logs/Warning.txt",
            "rollingInterval": "Day",
            "fileSizeLimitBytes": "2000000",
            "retainedFileCountLimit": "10",
            "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}|{EventId}|{Message}|{Scope} {NewLine}",
            "restrictedToMinimumLevel": "Warning"
        }
    }
}

Upvotes: 0

Views: 1144

Answers (1)

Alex Cr
Alex Cr

Reputation: 441

Thread from Serilog multiple files appsettings.json. My findings after a while of errors, retry and nearly giving up based on lack of documentation about Serilog. They have a tread on GitHub: https://github.com/serilog/serilog-filters-expressions/issues/27. Nearly every thread goes to the same conclusion that you have to create a SUBLOGGER. This is my implementation. For this implementation you need the following plugins:

  • Serilog Filter
  • Serilog Sink
  • Serilog Async

    "Serilog": {
    "Using": [ "Serilog.Sinks.File" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo:Information": { //this name here can be changed
      "Name": "Logger", //this name here is essential
      "Args": {
        "configureLogger": {
          "Filter": [
            {
              "Name": "ByIncludingOnly",
              "Args": {
                "expression": "@Level = 'Information'"
              }
            }
          ],
          "WriteTo": [
            {
              "Name": "Async", //i use async plugin from serilog
              "Args": {
                "configure": [
                  {
                    "Name": "File",
                    "Args": {
                      "path": "Logs/Log_.txt",
                      "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
                      "rollingInterval": "Day",
                      "retainedFileCountLimit": 7
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    },
    

Upvotes: 1

Related Questions