繁体   English   中英

使用 appsettings.json 配置 Serilog 以将某些日志过滤到不同的文件

[英]Configure Serilog to filter certain logs to a different file using appsettings.json

我有一个使用 Serilog 将日志数据写入文件的应用程序。 我现在正在尝试使用appsettings.json中的子记录器配置我的记录器,以便我可以将某些日志过滤到不同的文件中。 我知道我可以在代码中配置记录器,但我想通过appsettings.json来完成。

基本上,我希望所有日志到 go 到同一个文件,除了某种类型的日志。 我使用了Serilog wiki和一些博客文章和 stackoverflow 条目,主要是这个,来实现我的目标。从我读过的内容来看,使用以下内容应该允许我过滤这种类型的日志条目:

using (LogContext.PushProperty("SpecialLogType", true)) {
    _logger.LogInformation("MyLogEntry {MyParam}", myParam);
}

我配置了两个接收器,一个用于普通日志,一个用于这种特殊类型的日志。 使用过滤器,我现在应该能够使用此属性过滤日志。 但我无法弄清楚我需要如何在 appsettings.json 中配置子记录器。 现在,我的appsettings.json看起来像这样:

"Serilog": {
    "Using": [ "Serilog.Sinks.File", "Serilog.Settings.Configuration", "Serilog.Expressions" ],
    "MinimumLevel": {
      "Default": "Information",
    },
    "WriteTo": [
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByExcluding",
                "Args": {
                  "expression": "@p['SpecialLogType'] = 'true'"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/NormalTypeLog_.txt",
                  // other configuration
                }
              }
            ]
          }
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "@p['SpecialLogType'] = 'true'"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/SpecialTypeLog.json",
                  // other configuration
                }
              }
            ]
          }
        }
      }
    ]
  }

我已经尝试了几种不同的方法并取得了一些结果,但我无法让它正常工作并且会提出一些建议。 有没有人有任何提示?

所以几天后我设法弄清楚了这一点,并想如果有类似问题的人偶然发现它,我会在这里发布我的完整解决方案。

我认为我走在正确的道路上,但我的主要问题是我错误地使用了Serilog 的表达式package。 我将在多个示例中找到的东西混合在一起,但最终没有奏效。 更多阅读 wiki 和nblumhardt 的博客文章引导我找到最终的解决方案。

通过 Serilog 的 appsettings.json 配置过滤日志事件到不同的文件

可以使用属性丰富日志。 这些属性可以稍后在管道中用于将日志过滤到不同的记录器。 可以通过LogContext class 将属性添加到日志中。

using (LogContext.PushProperty("SpecialLogType", true)) {
    _logger.LogInformation("This is a log entry that will be filtered to a different file");
}

要实际配置记录器,您需要创建两个子记录器。 过滤必须发生在子记录器级别 每个子记录器都必须过滤它自己的事件。 在顶级记录器进行过滤将过滤掉所有子记录器的日志事件。 过滤是通过Serilog 的表达式package 完成的。

要仅包含附加属性的日志事件,我执行了以下操作

"Name": "ByIncludingOnly",
"Args": {
  "expression": "SpecialLogTypeis not null"
}

由于我希望将 go 的所有其他事件添加到“标准”文件中,因此对于我的其他子记录器,我只是排除了所有带有附加属性的日志事件

"Name": "ByExcluding",
"Args": {
  "expression": "SpecialLogTypeis not null"
}

完整的相关 appsettings.json 部分

  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Settings.Configuration", "Serilog.Expressions" ],
    "WriteTo": [
      { "Name": "Console" },
      { "Name": "Debug" },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByExcluding",
                "Args": {
                  "expression": "SpecialLogType is not null"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/NormalLogEvents_.txt",
                  // other irrelevant file configuration
                }
              }
            ]
          }
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "SpecialLogType is not null"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/SpecialLoggingType_.log",
                  // other irrelevant file configuration
                }
              }
            ]
          }
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
  }

这对我有用,我希望它可以帮助任何有同样问题的人。

也很有用: Serilog 设置配置自述文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM