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() == "/";
}