Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering dotnetcore health check logs

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.

like image 701
Tim Peel Avatar asked Mar 10 '20 16:03

Tim Peel


People also ask

How can I filter out health check request logs?

If the level you choose is lower than the minimum level, it will be filtered out completely and not logged. I showed that you could use this approach to filter out the common (but low-interest) request logs generated by calling health check endpoints.

What is aspnetcore diagnostics healthchecks?

AspNetCore.Diagnostics.HealthChecks includes metric-based health check scenarios, including disk storage and maximum value liveness checks. AspNetCore.Diagnostics.HealthChecks isn't maintained or supported by Microsoft.

What are health checks in ASP NET Core microservices?

When developing an ASP.NET Core microservice or web application, you can use the built-in health checks feature that was released in ASP .NET Core 2.2 ( Microsoft.Extensions.Diagnostics.HealthChecks ). Like many ASP.NET Core features, health checks come with a set of services and a middleware.

How to enable health check endpoint in DotNet core application?

Every dotnet core application implicitly refers a package Microsoft.AspNetCore.Diagnostics.HealthChecks package which makes it easy to add a basic health check to our application So, to enable health check endpoint we need to do below two code changes (highlighted in yellow) to our startup.cs file


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"
        }
      }
      ...
    ],
    ...
}
like image 189
hpsanampudi Avatar answered Sep 25 '22 11:09

hpsanampudi