© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
3 replies
Christopher Long

❔ 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
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() == "/";
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ Serilog
C#CC# / help
4y ago
✅ Serilog Confusion
C#CC# / help
11mo ago
❔ serilog problem
C#CC# / help
3y ago
application insight serilog
C#CC# / help
2y ago