Cannot filter out LogEvents using MinimumLevel.Override?

143 Views Asked by At

I have the following serilog config' code:

            loggerConfiguration
                .ReadFrom.Configuration(configuration)
                .MinimumLevel.Is(GetLoggingLevel(configuration.GetSection($"{Constants.LoggingSectionName}:MinimumLevel")))
                .MinimumLevel.Override("Microsoft.AspNetCore.Hosting.Diagnostics", LogEventLevel.Warning)
                .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning)
                .Enrich.FromLogContext()
                .WriteTo.Console(outputTemplate: Constants.Template,
                                 theme: AnsiConsoleTheme.Code,
                                 levelSwitch: HasLevelSwitches(configuration)
                                                ? new Serilog.Core.LoggingLevelSwitch(GetLoggingLevel(GetDictionaryOfSettings(configuration, "LevelSwitches")["$consoleLogLevel"]))
                                                : null)
                .WriteTo.File(path: configuration.GetSection($"{Constants.LoggingSectionName}:FilePath").Value,
                            rollingInterval: RollingInterval.Day,
                            retainedFileCountLimit: GetIntSafe(configuration.GetSection($"{Constants.LoggingSectionName}:RetainedFileCountLimit"), 31),
                            shared: true,
                            outputTemplate: Constants.Template,
                            fileSizeLimitBytes: ConvertMegaBytesToBytesSafe(configuration.GetSection($"{Constants.LoggingSectionName}:FileSizeLimitMegaBytes"), 500L),
                            rollOnFileSizeLimit: true,
                            flushToDiskInterval: GetTimeSpanFromSecondsSafe(configuration.GetSection($"{Constants.LoggingSectionName}:FlushToDiskIntervalSeconds")),
                            levelSwitch: HasLevelSwitches(configuration)
                                                ? new Serilog.Core.LoggingLevelSwitch(GetLoggingLevel(GetDictionaryOfSettings(configuration, "LevelSwitches")["$fileLogLevel"]))
                                                : null);

As you can see see, I have hard code some SourceContext namespaces.

My minimum log level is set to: Information

At Runtime, I consistently see log events generated from the two namespaces I have stated I would like filtering out less they be at LogLevel:Warning.

Update 1

I have tried very granular or loose and I have the same issue:

                .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                .MinimumLevel.Override("System", LogEventLevel.Warning)

Reserch

How can I override Serilog levels differently for different sinks?

1

There are 1 best solutions below

0
On

Was able to fix the issue using:

loggerConfiguration.Filter.ByExcluding(logEvent =>
            logEvent.Level < LogEventLevel.Warning &&
            Matching.FromSource("Microsoft.AspNetCore").Invoke(logEvent)

and stopped using:

.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)