How to split Serilog log file into multiple files?

40 views Asked by At

I'm trying to split logs by SourceContext. After spending some days and reading the documentation and many, many, many posts, I have this config and not a solution:

"Serilog": {
  "Using": [ "Serilog.Sinks.Debug", "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Expressions" ],
  "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
  "MinimumLevel": {
    "Default": "Debug",
    "Override": {
      "Microsoft.AspNetCore": "Information",
      "Microsoft": "Warning",
      "System": "Warning"
    }
  },
  "WriteTo": [
    {
      "Name": "File",
      "Args": {
        "path": "Logs/log-.txt",
        "rollingInterval": "Day",
        "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}"
      }
    },
    {
      "Name": "Logger",
      "Args": {
        "confgureLogger": {
          "WriteTo": [
            {
              "Name": "File",
              "Args": {
                "path": "Logs/log-HTTP-.txt",
                "rollingInterval": "Day",
                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {SourceContext} {Namespace} - {Message:lj}{NewLine}{Exception}"
              }
            }
          ],
          "Filter": [
            {
              "Name": "ByIncludingOnly",
              "Args": {
                "expression": "StartsWith(SourceContext, 'Microsoft.AspNetCore')"
              }
            }
          ]
        }
      }
    },
    {
      "Name": "Logger",
      "Args": {
        "confgureLogger": {
          "WriteTo": [
            {
              "Name": "File",
              "Args": {
                "path": "Logs/log-API-.txt",
                "rollingInterval": "Day",
                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {SourceContext} {Namespace} - {Message:lj}{NewLine}{Exception}"
              }
            }
          ],
          "Filter": [
            {
              "Name": "ByIncludingOnly",
              "Args": {
                "expression": "StartsWith(SourceContext, 'My.Application')"
              }
            }
          ]
        }
      }
    },

    {
      "Name": "Console",
      "Args": {
        "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {SourceContext} {NameSpace} - {Message:lj}{NewLine}{Exception}"
      }
    }
  ]
}

I've tried with "subLogs" and with multiple root logs.

With sublogs I have only one file. With rootlogs I have the same log in both files. I no longer know what to do other than change the logger and switch to NLog.

Is there a working solution?

0

There are 0 answers