❔ Serilog filtering

We’re getting a lot of service health check spam in our logs, and we’re only really interested in when things go wrong. So what I've tried so far is to creating this LoggerConfigurationExtensions class that has methods for the default config as well as the specific log filtering. This class lives in a nuget package that our services make use of.
public static class LoggerConfigurationExtensions
{
private static readonly string[] LogsToExclude =
{
"/health",
"TcpHealthProbeService",
"HealthCheckMessage"
};

public static LoggerConfiguration ApplyDefaultConfiguration(
this LoggerConfiguration configuration,
HostBuilderContext context,
IServiceProvider provider)
=> configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(provider)
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.WriteTo.Console(new JsonFormatter(renderMessage: true));

public static LoggerConfiguration IgnoreHealthChecksPath(this LoggerConfiguration configuration) =>
configuration.Filter.ByExcluding(log =>
log.Level is LogEventLevel.Information &&
(log.Properties.Any(prop => LogsToExclude.Contains(prop.Value.ToString())) ||
IsRequestLoggingMiddleware(log)));

private static bool IsRequestLoggingMiddleware(LogEvent log) =>
log.Properties.TryGetValue("SourceContext", out var sourceContextProp) &&
log.Properties.TryGetValue("RequestPath", out var requestPathProp) &&
sourceContextProp.ToString().Contains("RequestLoggingMiddleware") &&
requestPathProp.ToString() == "/";
}
public static class LoggerConfigurationExtensions
{
private static readonly string[] LogsToExclude =
{
"/health",
"TcpHealthProbeService",
"HealthCheckMessage"
};

public static LoggerConfiguration ApplyDefaultConfiguration(
this LoggerConfiguration configuration,
HostBuilderContext context,
IServiceProvider provider)
=> configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(provider)
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.WriteTo.Console(new JsonFormatter(renderMessage: true));

public static LoggerConfiguration IgnoreHealthChecksPath(this LoggerConfiguration configuration) =>
configuration.Filter.ByExcluding(log =>
log.Level is LogEventLevel.Information &&
(log.Properties.Any(prop => LogsToExclude.Contains(prop.Value.ToString())) ||
IsRequestLoggingMiddleware(log)));

private static bool IsRequestLoggingMiddleware(LogEvent log) =>
log.Properties.TryGetValue("SourceContext", out var sourceContextProp) &&
log.Properties.TryGetValue("RequestPath", out var requestPathProp) &&
sourceContextProp.ToString().Contains("RequestLoggingMiddleware") &&
requestPathProp.ToString() == "/";
}
3 Replies
Christopher Long
Christopher LongOP2y ago
In the services I'm implementing it like this in the program.cs
// Configure logging
Log.Logger = new LoggerConfiguration()
.Enrich
.FromLogContext()
.Enrich
.WithExceptionDetails()
.WriteTo.Console(new JsonFormatter())
.CreateBootstrapLogger();

builder.Host.UseSerilog((context, provider, configuration) =>
configuration
.ApplyDefaultConfiguration(context, provider)
.IgnoreHealthChecksPath());
// Configure logging
Log.Logger = new LoggerConfiguration()
.Enrich
.FromLogContext()
.Enrich
.WithExceptionDetails()
.WriteTo.Console(new JsonFormatter())
.CreateBootstrapLogger();

builder.Host.UseSerilog((context, provider, configuration) =>
configuration
.ApplyDefaultConfiguration(context, provider)
.IgnoreHealthChecksPath());
However no logs are being filtered out
Christopher Long
Christopher LongOP2y ago
an example log i'm trying to filter:
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server