Browse Source
Add `ExcludeExceptionFromLoggerSelectors` to `AbpExceptionHandlingOptions` to skip output exception to logger.
pull/22417/head
maliming
11 months ago
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4
4 changed files with
33 additions and
10 deletions
-
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs
-
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs
-
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs
-
framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs
|
|
|
@ -109,9 +109,12 @@ public class AbpExceptionFilter : IAsyncExceptionFilter, IAbpFilter, ITransientD |
|
|
|
remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); |
|
|
|
remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService<IJsonSerializer>().Serialize(remoteServiceErrorInfo, indented: true)); |
|
|
|
|
|
|
|
var logger = context.GetService<ILogger<AbpExceptionFilter>>(NullLogger<AbpExceptionFilter>.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<ILogger<AbpExceptionFilter>>(NullLogger<AbpExceptionFilter>.Instance)!; |
|
|
|
var logLevel = context.Exception.GetLogLevel(); |
|
|
|
logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); |
|
|
|
logger.LogException(context.Exception, logLevel); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -90,10 +90,12 @@ public class AbpExceptionPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDe |
|
|
|
remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); |
|
|
|
remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService<IJsonSerializer>().Serialize(remoteServiceErrorInfo, indented: true)); |
|
|
|
|
|
|
|
var logger = context.GetService<ILogger<AbpExceptionPageFilter>>(NullLogger<AbpExceptionPageFilter>.Instance)!; |
|
|
|
logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); |
|
|
|
|
|
|
|
logger.LogException(context.Exception!, logLevel); |
|
|
|
if (context.Exception != null && exceptionHandlingOptions.ShouldLogException(context.Exception)) |
|
|
|
{ |
|
|
|
var logger = context.GetService<ILogger<AbpExceptionPageFilter>>(NullLogger<AbpExceptionPageFilter>.Instance)!; |
|
|
|
logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); |
|
|
|
logger.LogException(context.Exception!, logLevel); |
|
|
|
} |
|
|
|
|
|
|
|
await context.GetRequiredService<IExceptionNotifier>().NotifyAsync(new ExceptionNotificationContext(context.Exception!)); |
|
|
|
|
|
|
|
@ -113,6 +115,7 @@ public class AbpExceptionPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDe |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var logger = context.GetService<ILogger<AbpExceptionPageFilter>>(NullLogger<AbpExceptionPageFilter>.Instance)!; |
|
|
|
logger.LogWarning("HTTP response has already started, cannot set headers and status code!"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -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<IOptions<AbpExceptionHandlingOptions>>().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<IExceptionToErrorInfoConverter>(); |
|
|
|
var statusCodeFinder = httpContext.RequestServices.GetRequiredService<IHttpExceptionStatusCodeFinder>(); |
|
|
|
var jsonSerializer = httpContext.RequestServices.GetRequiredService<IJsonSerializer>(); |
|
|
|
var exceptionHandlingOptions = httpContext.RequestServices.GetRequiredService<IOptions<AbpExceptionHandlingOptions>>().Value; |
|
|
|
|
|
|
|
httpContext.Response.Clear(); |
|
|
|
httpContext.Response.StatusCode = (int)statusCodeFinder.GetStatusCode(httpContext, exception); |
|
|
|
|
|
|
|
@ -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<Type> SendExceptionDataToClientTypes { get; set; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Selectors to exclude exception from logging.
|
|
|
|
/// If a selector returns true, the exception is not logged in to the logging.
|
|
|
|
/// </summary>
|
|
|
|
public List<Func<Exception, bool>> ExcludeExceptionFromLoggerSelectors { get; } |
|
|
|
|
|
|
|
public AbpExceptionHandlingOptions() |
|
|
|
{ |
|
|
|
SendExceptionsDetailsToClients = false; |
|
|
|
@ -19,5 +26,11 @@ public class AbpExceptionHandlingOptions |
|
|
|
[ |
|
|
|
typeof(IBusinessException) |
|
|
|
]; |
|
|
|
ExcludeExceptionFromLoggerSelectors = new List<Func<Exception, bool>>(); |
|
|
|
} |
|
|
|
|
|
|
|
public bool ShouldLogException(Exception exception) |
|
|
|
{ |
|
|
|
return ExcludeExceptionFromLoggerSelectors.All(selector => !selector(exception)); |
|
|
|
} |
|
|
|
} |
|
|
|
|