Christopher Long
Christopher Long
Explore posts from servers
CC#
Created by Christopher Long on 3/21/2023 in #help
❔ 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() == "/";
}
4 replies