Filtering dotnetcore health check logs

Viewed 5832

Is it possible to filter logs using the default dotnetcore logging specific to health check requests?

So this would be ignoring requests to paths such as /health

I can see the filtering available by category and type but this is not enough, as it would prevent logging for other requests too.

Alternatives such as Serilog can do similar, such as this guide: https://andrewlock.net/using-serilog-aspnetcore-in-asp-net-core-3-excluding-health-check-endpoints-from-serilog-request-logging/

Failing that it could well be a case of a custom logger.

1 Answers

In .net core 3.1 using serilog we can filter the health checks by adopting anyone if the below options.

Requirement: Exclude all health check logs which are healthy.

Note: For health checks UI, I am using InMemoryStorage as storage provider.

Option 1 : It is simple, if we are not interested about EF core logs which are having LogLevel information and below

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

Option 2 : If we are concern about EF core logs which are having LogLevel information and above then we need to exclude EF logs related to health checks explicitly.

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