diff --git a/SharedKernel.Demo/SharedKernel.Demo.csproj b/SharedKernel.Demo/SharedKernel.Demo.csproj index 931dd8c..2feee98 100644 --- a/SharedKernel.Demo/SharedKernel.Demo.csproj +++ b/SharedKernel.Demo/SharedKernel.Demo.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/SharedKernel/Logging/SerilogExtensions.cs b/src/SharedKernel/Logging/SerilogExtensions.cs index 67fae8e..0eba751 100644 --- a/src/SharedKernel/Logging/SerilogExtensions.cs +++ b/src/SharedKernel/Logging/SerilogExtensions.cs @@ -21,12 +21,13 @@ public static WebApplicationBuilder AddSerilog(this WebApplicationBuilder builde LogBackend logBackend, Dictionary? logAdditionalProperties = null, int daysToRetain = 7, - bool asyncSinks = false) + bool asyncSinks = false, + bool suppressAspNetExceptionHandler = true) { builder.Logging.ClearProviders(); var loggerConfig = new LoggerConfiguration() - .FilterOutUnwantedLogs() + .FilterOutUnwantedLogs(suppressAspNetExceptionHandler) .Enrich .FromLogContext() .ConfigureDestinations(builder, logBackend, asyncSinks) @@ -159,13 +160,35 @@ private static LoggerConfiguration WriteToFile(this LoggerConfiguration loggerCo return loggerConfig.WriteTo.File(formatter, logPath, rollingInterval: RollingInterval.Day); } - private static LoggerConfiguration FilterOutUnwantedLogs(this LoggerConfiguration loggerConfig) + private static LoggerConfiguration FilterOutUnwantedLogs(this LoggerConfiguration loggerConfig, + bool suppressAspNetExceptionHandler) { - return loggerConfig - .Filter - .ByExcluding(IsEfOutboxQuery) - .Filter - .ByExcluding(ShouldDropByPath); + var filteredConfig = loggerConfig + .Filter + .ByExcluding(IsEfOutboxQuery) + .Filter + .ByExcluding(ShouldDropByPath); + if (suppressAspNetExceptionHandler) + { + filteredConfig = filteredConfig + .Filter + .ByExcluding(ShouldDropExceptionHandlerLogs); + } + + return filteredConfig; + } + + private static bool ShouldDropExceptionHandlerLogs(LogEvent evt) + { + if (!evt.Properties.TryGetValue("SourceContext", out var sc) || sc is not ScalarValue sv) + { + return false; + } + + var sourceContext = sv.Value as string ?? ""; + + return sourceContext.Equals("Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware", + StringComparison.OrdinalIgnoreCase); } private static bool ShouldDropByPath(LogEvent evt) diff --git a/src/SharedKernel/SharedKernel.csproj b/src/SharedKernel/SharedKernel.csproj index c5db731..9cbe786 100644 --- a/src/SharedKernel/SharedKernel.csproj +++ b/src/SharedKernel/SharedKernel.csproj @@ -8,13 +8,13 @@ Readme.md Pandatech MIT - 1.8.4 + 1.8.5 Pandatech.SharedKernel Pandatech Shared Kernel Library Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document. https://github.com/PandaTechAM/be-lib-sharedkernel - tag ordering added to open api + logging option to remove duplicate logs @@ -39,13 +39,13 @@ - + - - + + - - + + diff --git a/test/SharedKernel.Tests/SharedKernel.Tests.csproj b/test/SharedKernel.Tests/SharedKernel.Tests.csproj index add6b8a..0218235 100644 --- a/test/SharedKernel.Tests/SharedKernel.Tests.csproj +++ b/test/SharedKernel.Tests/SharedKernel.Tests.csproj @@ -10,9 +10,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all