繁体   English   中英

Serilog 过滤在 appsettings.json 中不起作用

[英]Serilog Filtering not working in appsettings.json

我实现了 Serilogger 并将配置放在 Program.cs 中,效果很好。 我正在尝试将配置移动到 appsettings.json 并将其写入日志,但我现在无法让过滤器工作。 我知道它正在读取配置,因为我可以更改最低日志级别,并在日志中看到结果。

我一直在阅读和重读 Github 中的 Serilog 页面,尤其是配置页面

这是在 Program.cs 中工作的代码

            try
            {
                Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));



                var levelSwitch = new LoggingLevelSwitch();
                levelSwitch.MinimumLevel = LogEventLevel.Warning;

                return new LoggerConfiguration()
                    .ReadFrom.Configuration(config, "Serilog");

                    .MinimumLevel.ControlledBy(levelSwitch)
                    .Enrich.FromLogContext()
                    .WriteTo.Debug()
                    .WriteTo.Logger(l => l
                        .Filter.ByExcluding("source = 'application'")
                        .WriteTo.File("Logs\\log-.txt", rollingInterval: RollingInterval.Day))
                    .WriteTo.Logger(l => l
                        .Filter.ByIncludingOnly("source='customer'")
                        .WriteTo.MSSqlServer(
                            connectionString:
                            "Server=(localdb)\\Infinity;Database=Infinity;Trusted_Connection=True;MultipleActiveResultSets=True;",
                            sqlSinkOptions));
            }

这是新的 appsettings 文件

{
  "Serilog": {
    "Using": [ "Serilog.Enrichers.FromLogContext" ],
    "MinimumLevel": "Warning",
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "Server=(localdb)\\Infinity;Database=Infinity;Trusted_Connection=True;MultipleActiveResultSets=True;",
          "tableName": "Log",
          "Filter": [
            {
              "Name": "ByIncludingOnly",
              "Args": {
                "expression": "source = 'customer'"
              }
            }
          ]
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "Logs\\log-.txt",
          "rollingInterval": "Day",
          "retainedFileCountLimit": 7,
          "Filter": [
            {
              "Name": "ByExcluding",
              "Args": {
                "expression": "source = 'customer'"
              }
            }
          ]
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  },

更新:我发现我正在过滤的自定义属性不再插入到记录器对象中。 我认为这与“丰富者”有关吗? 但它不是过滤器问题的根源,因为如果属性不存在,“IncludeOnly”过滤器不应该写任何东西,对吗?

这是我使用 PushProperty 插入属性和值的方法的片段。 由于我没有接触过这段代码,我认为问题仍然存在于应用程序设置中。


        public void LogError(LogDestination destination, LogLevel level, string msg)
        {
            if (destination == LogDestination.Customer)
            {                
                using (LogContext.PushProperty("source", "customer")) {
                    switch (level)
                    {
                        case LogLevel.Debug:
                            _logger.LogInformation(msg);
                            break;

我也尝试将 .Enrich 添加回 LoggerConfiguration ,没有任何区别。

  return new LoggerConfiguration()
                    .ReadFrom.Configuration(config, "Serilog")
                    .Enrich.FromLogContext();

更新我在顶层添加了一个全局过滤器。 我尝试了 Include Only 和 Exclude ,两者都按预期工作。 我无法弄清楚为什么水槽过滤器不起作用。

{
  "Serilog": {
    "Using": [ "Serilog.Expressions", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
    "MinimumLevel": "Warning",
    "Filter": [
      {
        "Name": "ByExluding",
        "Args": {
          "expression": "source = 'customer'"
        }
      }
    ],

我尝试在 Appsettings 文件中使用过滤器而不是过滤器,它对我有用。 但是没有看到任何有关它的文档。

{
  "Serilog": {
    "Using": [ "Serilog.Expressions", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
    "MinimumLevel": "Warning",
    "Filters": [
      {
        "Name": "ByExluding",
        "Args": {
          "expression": "source = 'customer'"
        }
      }
    ],

暂无
暂无

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

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