0

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"
        }
    }
}
3

1 Answer 1

1

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
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    },
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.