Serilog 过滤器 RequestPath

Serilog filter RequestPath

我无法从 serilog 文件中排除健康检查。

  1. Serilog.Filters.Expressions 已安装,StatusCode=200 行将被过滤。

  2. 应用程序设置:

    "Serilog": {
         "MinimumLevel": {
           "Default": "Verbose",
           "Override": {
             "Microsoft": "Information",
         "Microsoft.EntityFrameworkCore": "Warning",
         "AspNetCore.HealthChecks.UI": "Warning",
             "HealthChecks": "Warning",
             "System": "Warning"
           }
         },
         "Using": ["Serilog.Filters.Expressions"],
         "Filter": [
           {
             "Name": "ByExcluding",
             "Args": {
               "expression": "EndsWith(RequestPath, '/healthcheck') and StatusCode=200"
             }
           }
         ],
         "WriteTo": [
           {
             "Name": "Async",
             "Args": {
               "configure": [
                 {
                   "Name": "File",
                   "Args": {
                     "path": "C:/Logs/MyApi/log.txt",
                     "rollingInterval": "Day",
                     "retainedFileCountLimit": 7,
                     "buffered": true
                   }
                 }
               ]
             }
           }
         ]
       }
    

3.Programm

 public class Program
    {
        public static int Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .CreateLogger();
            try
            {
                Log.Information("Starting web host");
                CreateWebHostBuilder(args).Build().Run();
                return 0;
            }
            catch (Exception ex)
            { Log.Fatal(ex, "Host terminated unexpectedly");
                return 1;
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>

       WebHost.CreateDefaultBuilder(args)
               .UseStartup<Startup>()
               .UseSerilog((hostingContext, loggerConfiguration) =>
               {
                   loggerConfiguration.Enrich.FromLogContext()
                     .WriteTo.Elasticsearch()
                   .ReadFrom.Configuration(hostingContext.Configuration);
                   Log.Information("App started");
               });

日志更改自

2020-11-04 23:59:44.128 +01:00 [INF] Request starting HTTP/1.1 GET https://servicesp-rbg.company.com/MyApi/healthcheck 2020-11-04 23:59:44.128 +01:00 [INF] Request finished in 0.211ms 200 text/plain

2020-11-06 14:27:32.787 +01:00 [INF] Request starting HTTP/1.1 GET https://servicesp-rbg.company.com/My.Api/healthcheck

因此 statuscode=200 过滤器正在工作,但 RequestPath 没有工作 'healthcheck'。

简单的解决方案 "expression": "EndsWith(RequestPath, '/healthcheck') AND St​​atusCode=200"

AND 替换为 OR

“表达式”:“EndsWith(RequestPath, '/healthcheck') OR StatusCode=200”