2

I trying to configure Serilog to separate normal logs and entity framework logging. Here is my serilog configuration:

"Serilog": {
"Using": [
  "Serilog.Settings.Configuration",
  "Serilog.Sinks.File"
],
"MinimumLevel": {
  "Default": "Debug",
  "Override": {
    "Microsoft": "Debug",
    "System": "Warning"
  }
},
"WriteTo": [
  {
    "Name": "RollingFile",
    "Args": {
      "configureLogger": {
        "WriteTo": [
          {
            "Name": "LogFile",
            "Args": {
              "textFormatter": "JsonFormatter",
              "fileSizeLimitBytes": 2147483648,
              "retainedFileCountLimit": 5
            }
          }
        ]
      },
      "pathFormat": "log-{Date}.log",
      "logDirectory": ".",
      "Filter": [
        {
          "Name": "ByExcluding",
          "Args": {
            "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore.')"
          }
        }
      ]
    }
  },
  {
    "Name": "RollingFile2",
    "Args": {
      "configureLogger": {
        "WriteTo": [
          {
            "Name": "LogFile2",
            "Args": {
              "textFormatter": "JsonFormatter",
              "fileSizeLimitBytes": 2147483648,
              "retainedFileCountLimit": 5
            }
          }
        ]
      },
      "pathFormat": "log-DB-{Date}.log",
      "logDirectory": ".",
      "Filter": [
        {
          "Name": "ByIncluding",
          "Args": {
            "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore.')"
          }
        }
      ]
    }
  },
  {
    "Name": "Console",
    "Args": {
      "outputTemplate": "{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}"
    }
  }
],
"Enrich": [
  "FromLogContext",
  "WithMachineName",
  "WithProcessId",
  "WithThreadId",
  "WithHttpRequestId"
]

}

I do not get a second log file. I base myself on this post to realize my configuration Filter Serilog logs to different sinks depending on context source?

What did I not understand in the serilog configuration? I find a lot of configuration examples via the code but very little via the appsettings.json. I prefer to use appsetting.json

Thanks.

Xarkam
  • 121
  • 7

1 Answers1

3

You have to setup up sub loggers, not rolling files. You have two entries in your WriteTo array, but both are "Name": "RollingFile". Change them to be "Name": "Logger" and then move the filter configs into the respective configureLogger sections of these sub loggers. Something like this:

    "WriteTo": [
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "log-{Date}.log",
                  "retainedFileCountLimit": 5,
                  "fileSizeLimitBytes": 2147483648,
                }
              }
            ],
            "Filter": [
              {
                "Name": "ByExcluding",
                "Args": {
                  "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore.')"
                }
              }
            ]
          }
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "log-db-{Date}.log",
                  "retainedFileCountLimit": 5,
                  "fileSizeLimitBytes": 2147483648,
                }
              }
            ],
            "Filter": [
              {
                "Name": "ByIncluding",
                "Args": {
                  "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore.')"
                }
              }
            ]
          }
        }
      }
    ],

In my tests there are still some Microsoft.AspNetCore and Microsoft.Extensions namespaces coming through in the second filter - so not sure if ByIncluding is blocking like ByExcluding is. And I'm not sure what other filter types are available for this.

Update

Just saw elsewhere that this appears to another type of filter that you can try: "Name": "ByIncludingOnly".

helloserve
  • 1,248
  • 12
  • 14