From a2df86fa4ae6c16cc8ff595d2f2877aa768d88b0 Mon Sep 17 00:00:00 2001 From: Haik Date: Thu, 30 Oct 2025 17:37:31 +0400 Subject: [PATCH] duplicate exception logs removal --- SharedKernel.Demo/SharedKernel.Demo.csproj | 2 +- src/SharedKernel/Logging/SerilogExtensions.cs | 39 +++++++++++++++---- src/SharedKernel/SharedKernel.csproj | 14 +++---- .../SharedKernel.Tests.csproj | 4 +- 4 files changed, 41 insertions(+), 18 deletions(-) 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