1

I want my logs to persist in different folders based on their level. And I want this configuration to be completely via config file rather than hard-coded configs as it may change in future. Doing so in NLog configuration file is pretty easy, but I didn't find it how to do advanced log configuration in Serilog. Any suggestion?

Currently I have this:

"Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": "Information",
    "Override": {
      "Microsoft": "Warning",
      "System": "Warning"
    },
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "File",
        "Args": {
          "path": "logs\\AppLogs_.txt",
          "rollingInterval": "Hour",
          "fileSizeLimitBytes": 15000000,
          "rollOnFileSizeLimit": true,
          "shared": true,
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}]) {Message} {NewLine} {Exception}"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]


  }
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
  • You can override it on a per sink level according to the [documentation](https://github.com/serilog/serilog/wiki/Configuration-Basics#overriding-per-sink) – Simply Ged Sep 02 '19 at 03:37
  • @SimplyGed, that's a code change, as I said, I want to config it per level in conifguration, I didn't find a sample config file for per level config – Mohsen Afshin Sep 02 '19 at 05:19
  • Have you tried adding `"restrictedLevelMinimum": "Debug"` to the config `Args`? – Simply Ged Sep 02 '19 at 05:24
  • Also, as that document says, you can't change the config level on a per-sink basis: From the docs `the logger-level configuration controls which logging statements will result in the creation of events, while the sink-level configuration only filters these. To create a single logger with a more verbose level, use a separate LoggerConfiguration.` – Simply Ged Sep 02 '19 at 05:27
  • `restrictedLevelMinimum` didn't work – Mohsen Afshin Sep 03 '19 at 03:43

1 Answers1

0

This is a very old post but for anyone having this problem here is my solution.

You need to create a config file for each level you want separated. I have:

Serilog.Debug.json
Serilog.Information.json
Serilog.Error.json
Serilog.Warning.json

In these 4 files you need to have the following confinguration:

{
  "Serilog:WriteTo:3:Args:configureLogger": { //notice this key
     "WriteTo": [
     {
         "Name": "File",
         "Args": {
         "path": "some path containing the log level"
          }
     }
     ],
"Filter": [
  {
    "Name": "ByIncludingOnly",
    "Args": {
      "expression": "@l = 'Debug'"
     }
   }
   ]
}
}

Then you can have another file for Fatal Serilog.json and the following content:

{
"Serilog": {
"Enrich": [
  "FromLogContext",
  "WithMachineName"
],
"restrictedToMinimumLevel": null,
"WriteTo": [
  {
    "Name": "File",
    "Args": {
      "path": "Logs/Fatal_.txt",
      "restrictedToMinimumLevel": "Fatal"
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {} // leave this empty: Information
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {} // leave this empty: Warning
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {} // leave this empty: Debug
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {} // leave this empty: Error
    }
  }
]
}
}  

In order for all these to work you need at least the following packages:

Serilog.Sinks.File
Serilog.Expressions
Serilog.Expressions.Logging
sebastian.roibu
  • 2,579
  • 7
  • 37
  • 59