Filter Serilog logs to different sinks depending on context source?

后端 未结 2 745
小蘑菇
小蘑菇 2020-12-13 00:56

I have a .NET Core 2.0 application in which I successfully use Serilog for logging. Now, I would like to log some database performance statistics to a separate sink (they ar

2条回答
  •  攒了一身酷
    2020-12-13 01:22

    Writing only entityframework log to a file is done below but we need to install Serilog.Filters.Expressions nuget package

    "Serilog": {
        "MinimumLevel": {
          "Default": "Debug",
          "Override": {
            "Microsoft": "Warning",
            "System": "Warning",
            "Microsoft.EntityFrameworkCore": "Information"
          }
        },
        "WriteTo": [
          {
            "Name": "Logger",
            "Args": {
              "configureLogger": {
                "Filter": [
                  {
                    "Name": "ByIncludingOnly",
                    "Args": {
                      "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore')"
                    }
                  }
                ],
                "WriteTo": [
                  {
                    "Name": "File",
                    "Args": {
                      "path": "C:\\LOGS\\TestService_EF_.json",
                      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
                      "rollingInterval": "Day",
                      "retainedFileCountLimit": 7
                    }
                  }
                ]
              }
            }
          }
        ]
      }
    

提交回复
热议问题