过滤dotnetcore运行状况检查日志

时间:2020-03-10 16:27:03

标签: .net-core asp.net-core-3.1

是否可以使用特定于运行状况检查请求的默认dotnetcore日志来过滤日志?

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

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

诸如Serilog之类的替代方法可以执行类似的操作,例如本指南:https://andrewlock.net/using-serilog-aspnetcore-in-asp-net-core-3-excluding-health-check-endpoints-from-serilog-request-logging/

如果没有可能是自定义记录器的情况。

1 个答案:

答案 0 :(得分:3)

在使用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"
        }
      }
      ...
    ],
    ...
}

选项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"
        }
      }
      ...
    ],
    ...
}