过滤 dotnetcore 运行状况检查日志

Tim*_*eel 7 .net-core asp.net-core-3.1

是否可以使用特定于健康检查请求的默认 dotnetcore 日志来过滤日志?

因此,这将忽略对 /health 等路径的请求

我可以看到按类别和类型可用的过滤,但这还不够,因为它也会阻止记录其他请求。

Serilog 等替代品可以做类似的事情,例如本指南:https ://andrewlock.net/using-serilog-aspnetcore-in-asp-net-core-3- exclude-health-check-endpoints-from-serilog-request -记录/

如果失败,很可能是自定义记录器的情况。

hps*_*udi 6

在使用 serilog 的 .net core 3.1 中,如果以下选项,我们可以通过采用任何人来过滤健康检查。

要求:排除所有健康的健康检查日志。

注意:对于健康检查 UI,我使用 InMemoryStorage 作为存储提供程序。

选项 1:这很简单,如果我们对具有 LogLevel 信息及以下信息的 EF 核心日志不感兴趣

appsetting.json

"Serilog": {
    "MinimumLevel": {
      "Default": "Verbose",
      //Hp --> Logic: Override filters out logs that are all below the configured log level
      "Override": {
        ...
        "Microsoft.EntityFrameworkCore": "Warning",
        "AspNetCore.HealthChecks.UI": "Warning",
        "HealthChecks": "Warning"
        ...
      }
    },
    "Filter": [
      ...
      { //Hp --> Logic: Filters all health check logs which are healthy
        "Name": "ByExcluding",
        "Args": {
          "expression": "EndsWith(RequestPath, '/healthcheck') and StatusCode=200"
        }
      }
      ...
    ],
    ...
}
Run Code Online (Sandbox Code Playgroud)

选项 2:如果我们关注具有 LogLevel 信息及以上信息的 EF 核心日志,那么我们需要明确排除与健康检查相关的 EF 日志。

"Serilog": {
    "MinimumLevel": {
      "Default": "Verbose",
      //Hp --> Logic: Override filters out logs that are all below the configured log level
      "Override": {
        ...
        "Microsoft.EntityFrameworkCore": "Information",
        "AspNetCore.HealthChecks.UI": "Warning",
        "HealthChecks": "Warning"
        ...
      }
    },
    "Filter": [
      ...
      {
        "Name": "ByExcluding",
        "Args": {
          "expression": "contextType='HealthChecksDb' or options='StoreName=HealthChecksUI '"
        }
      },
      {
        "Name": "ByExcluding",
        "Args": {
          "expression": "Scope[?] = 'HealthReportCollector is collecting health checks results.'"
        }
      },
      { //Hp --> Logic: Exclude all logs which are related to database instance health checks
        "Name": "ByExcluding",
        "Args": {
          "expression": "HealthCheckName='<xxx>DbContext' and StartsWith(EventId.Name,'Microsoft.EntityFrameworkCore')"
        }
      },
      { //Hp --> Logic: Filters all health check logs which are healthy
        "Name": "ByExcluding",
        "Args": {
          "expression": "EndsWith(RequestPath, '/healthcheck') and StatusCode=200"
        }
      }
      ...
    ],
    ...
}
Run Code Online (Sandbox Code Playgroud)

  • 另请注意,有一个新的 Serilog.Expressions 包可供 Filter 使用,https://github.com/serilog/serilog-expressions (2认同)