I have problems to exclude healthchecks from serilog files.
Serilog.Filters.Expressions are installed and the StatusCode=200 line will be filtered.
appsettings:
"Serilog": {
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"Microsoft": "Information",
"Microsoft.EntityFrameworkCore": "Warning",
"AspNetCore.HealthChecks.UI": "Warning",
"HealthChecks": "Warning",
"System": "Warning"
}
},
"Using": ["Serilog.Filters.Expressions"],
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "EndsWith(RequestPath, '/healthcheck') and StatusCode=200"
}
}
],
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "File",
"Args": {
"path": "C:/Logs/MyApi/log.txt",
"rollingInterval": "Day",
"retainedFileCountLimit": 7,
"buffered": true
}
}
]
}
}
]
}
3.Programm
public class Program
{
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
try
{
Log.Information("Starting web host");
CreateWebHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{ Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog((hostingContext, loggerConfiguration) =>
{
loggerConfiguration.Enrich.FromLogContext()
.WriteTo.Elasticsearch()
.ReadFrom.Configuration(hostingContext.Configuration);
Log.Information("App started");
});
The logs changed from
2020-11-04 23:59:44.128 +01:00 [INF] Request starting HTTP/1.1 GET https://servicesp-rbg.company.com/MyApi/healthcheck 2020-11-04 23:59:44.128 +01:00 [INF] Request finished in 0.211ms 200 text/plain
to
2020-11-06 14:27:32.787 +01:00 [INF] Request starting HTTP/1.1 GET https://servicesp-rbg.company.com/My.Api/healthcheck
so the statuscode=200 filter is working but not the RequestPath 'healthcheck'.
Easy solution "expression": "EndsWith(RequestPath, '/healthcheck') AND StatusCode=200"
Replaced AND by OR
"expression": "EndsWith(RequestPath, '/healthcheck') OR StatusCode=200"