Serilog筛选器表达式无法排除给定的端点日志

时间:2020-02-21 21:15:15

标签: asp.net-core logging serilog

我在.NetCore(2.2)WebAPI项目中有一个运行状况检查终结点,因此我不想为其生成和写入任何日志。我正在将日志写入WindowsEventLog。为此,我只是为了启动时加载的日志记录目的而使用了单独的配置文件。

这是我在控制器中进行健康检查的端点:

[Route("healthcheck")] 
[AllowAnonymous]
public IActionResult Get()
{
    return Ok("");
}

这是我如何加载为保存Serilog配置而创建的配置文件(SeriLogSettings.json):

 public Startup(IConfiguration configuration, IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
               .SetBasePath(env.ContentRootPath)
               .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
               .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
               .AddJsonFile("SeriLogSettings.json", optional: false)
               .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

这是我的SerilogSettings.json的样子:

{
  "Serilog": {
    "Using": [ "Serilog.Settings.Configuration", "Serilog.Sinks.EventLog" ],
    "Filter": [
      {
        "Name": "ByExcluding",
        "Args": {
          "expression": "EndsWith(RequestPath, '%/healthcheck')"
        }
      }
    ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "Enrich": [ "FromLogContext" ],
    "WriteTo": [
      {
        "Name": "EventLog",
        "Args": {
          "source": "WebAPI", 
          "logName": "Application",
          "managedEventSource": true
        }
      }
    ],
    "Properties": {
      "Application": "ApplicationAPI"
    }
  }
}

尽管使用ByExclusion表达式来排除/ healthcheck端点的日志,但我正在WindowsEventLog中获取日志。 我无法解决为什么会发生这种情况,并且不确定我在做什么错?

1 个答案:

答案 0 :(得分:0)

问题是您要检查的路径中的%字符,应该只是/healthcheck

"expression": "EndsWith(RequestPath, '/healthcheck')"

如果您在GitHub页面上查看the documentation,您会发现他们在测试请求路径时也只提到/SomeEndpoint