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

问题描述 投票:0回答:1

我的.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中获取日志。我无法解决发生这种情况的原因,并且不确定我在做什么错吗?

asp.net-core logging serilog
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.