为 serilog sink 指定更高的日志级别

Specifying higher log level for serilog sink

我的 ASP.NET WEB API .NET Core 3 应用程序中有许多 Serilog 接收器。 其中一个接收器是 MS SQL,我只想在其中写入错误消息。这是我的一部分 appsettings.json

"Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.Debug", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Information",
        "System": "Information"
      }
    },
    "WriteTo": [
      { "Name": "Console" },
      { "Name": "Debug" },
      {
        "Name": "File",
        "Args": {
          "path": "Logs/log_.txt",
          "rollingInterval": "Minute",
          "shared": true
        }
      },
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "CONNECTIONSTRING",
          "sinkOptionsSection": {
            "tableName": "Logs",
            "schemaName": "EventLogging",
            "autoCreateSqlTable": true,
            "batchPostingLimit": 50,
            "restrictedToMinimumLevel": "Error",
            "period": "0.00:00:30"
          }
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName" ]
  }

但最终我在 SQL 日志 table 中收到了一些信息和调试级别的消息 table。

上面的配置有什么问题?为什么 restrictedToMinimumLevel 没有帮助?

如果我想排除低于 Information 的消息,我必须在 Default 部分的 MinimumLevel 部分中进行设置。

但在这种情况下,我不允许为特定接收器指定较低级别的消息。例如

"restrictedToMinimumLevel":"Debug"

无济于事 - 调试消息不会写入 MS SQL。

restrictedToMinimumLevelArgs 的 child。 检查 docs.

尝试:

"WriteTo": [
  { "Name": "Console" },
  { "Name": "Debug" },
  {
    "Name": "File",
    "Args": {
      "path": "Logs/log_.txt",
      "rollingInterval": "Minute",
      "shared": true
    }
  },
  {
    "Name": "MSSqlServer",
    "Args": {
      "connectionString": "CONNECTIONSTRING",
      "sinkOptionsSection": {
        "tableName": "Logs",
        "schemaName": "EventLogging",
        "autoCreateSqlTable": true,
        "batchPostingLimit": 50,            
        "period": "0.00:00:30"
      },
      "restrictedToMinimumLevel": "Error",
    }
  }
],