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