From d6f5784bd99b0a4df7c58eb8249e31f72517fedf Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 21 Mar 2025 11:00:11 +0800 Subject: [PATCH] Add `ExcludeExceptionFromLoggerSelectors` to `AbpExceptionHandlingOptions` to skip output exception to logger. --- .../Mvc/ExceptionHandling/AbpExceptionFilter.cs | 11 +++++++---- .../Mvc/ExceptionHandling/AbpExceptionPageFilter.cs | 11 +++++++---- .../AbpExceptionHandlingMiddleware.cs | 8 ++++++-- .../AbpExceptionHandlingOptions.cs | 13 +++++++++++++ 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs index f09cde9815..1c616e7a8a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs @@ -109,9 +109,12 @@ public class AbpExceptionFilter : IAsyncExceptionFilter, IAbpFilter, ITransientD remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService().Serialize(remoteServiceErrorInfo, indented: true)); - var logger = context.GetService>(NullLogger.Instance)!; - var logLevel = context.Exception.GetLogLevel(); - logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); - logger.LogException(context.Exception, logLevel); + if(exceptionHandlingOptions.ShouldLogException(context.Exception)) + { + var logger = context.GetService>(NullLogger.Instance)!; + var logLevel = context.Exception.GetLogLevel(); + logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); + logger.LogException(context.Exception, logLevel); + } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs index 0e53b01cad..d8300eef04 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs @@ -90,10 +90,12 @@ public class AbpExceptionPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDe remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService().Serialize(remoteServiceErrorInfo, indented: true)); - var logger = context.GetService>(NullLogger.Instance)!; - logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); - - logger.LogException(context.Exception!, logLevel); + if (context.Exception != null && exceptionHandlingOptions.ShouldLogException(context.Exception)) + { + var logger = context.GetService>(NullLogger.Instance)!; + logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); + logger.LogException(context.Exception!, logLevel); + } await context.GetRequiredService().NotifyAsync(new ExceptionNotificationContext(context.Exception!)); @@ -113,6 +115,7 @@ public class AbpExceptionPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDe } else { + var logger = context.GetService>(NullLogger.Instance)!; logger.LogWarning("HTTP response has already started, cannot set headers and status code!"); } diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs index c99ac00fcc..c098ba2c15 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs @@ -58,7 +58,12 @@ public class AbpExceptionHandlingMiddleware : AbpMiddlewareBase, ITransientDepen private async Task HandleAndWrapException(HttpContext httpContext, Exception exception) { - _logger.LogException(exception); + var exceptionHandlingOptions = httpContext.RequestServices.GetRequiredService>().Value; + + if (exceptionHandlingOptions.ShouldLogException(exception)) + { + _logger.LogException(exception); + } await httpContext .RequestServices @@ -77,7 +82,6 @@ public class AbpExceptionHandlingMiddleware : AbpMiddlewareBase, ITransientDepen var errorInfoConverter = httpContext.RequestServices.GetRequiredService(); var statusCodeFinder = httpContext.RequestServices.GetRequiredService(); var jsonSerializer = httpContext.RequestServices.GetRequiredService(); - var exceptionHandlingOptions = httpContext.RequestServices.GetRequiredService>().Value; httpContext.Response.Clear(); httpContext.Response.StatusCode = (int)statusCodeFinder.GetStatusCode(httpContext, exception); diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs index e3bde2e813..cc751fc621 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Volo.Abp.AspNetCore.ExceptionHandling; @@ -11,6 +12,12 @@ public class AbpExceptionHandlingOptions public List SendExceptionDataToClientTypes { get; set; } + /// + /// Selectors to exclude exception from logging. + /// If a selector returns true, the exception is not logged in to the logging. + /// + public List> ExcludeExceptionFromLoggerSelectors { get; } + public AbpExceptionHandlingOptions() { SendExceptionsDetailsToClients = false; @@ -19,5 +26,11 @@ public class AbpExceptionHandlingOptions [ typeof(IBusinessException) ]; + ExcludeExceptionFromLoggerSelectors = new List>(); + } + + public bool ShouldLogException(Exception exception) + { + return ExcludeExceptionFromLoggerSelectors.All(selector => !selector(exception)); } }