Browse Source

Merge pull request #22417 from abpframework/ExcludeExceptionFromLoggerSelectors

Add `ExcludeExceptionFromLoggerSelectors` to `AbpExceptionHandlingOptions` to skip output exception to logger.
pull/22420/head
Engincan VESKE 2 years ago
committed by GitHub
parent
commit
0c2f4ec9fd
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 11
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs
  2. 11
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs
  3. 8
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs
  4. 13
      framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs

11
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($"---------- {nameof(RemoteServiceErrorInfo)} ----------");
remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService<IJsonSerializer>().Serialize(remoteServiceErrorInfo, indented: true)); remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService<IJsonSerializer>().Serialize(remoteServiceErrorInfo, indented: true));
var logger = context.GetService<ILogger<AbpExceptionFilter>>(NullLogger<AbpExceptionFilter>.Instance)!; if(exceptionHandlingOptions.ShouldLogException(context.Exception))
var logLevel = context.Exception.GetLogLevel(); {
logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); var logger = context.GetService<ILogger<AbpExceptionFilter>>(NullLogger<AbpExceptionFilter>.Instance)!;
logger.LogException(context.Exception, logLevel); var logLevel = context.Exception.GetLogLevel();
logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString());
logger.LogException(context.Exception, logLevel);
}
} }
} }

11
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($"---------- {nameof(RemoteServiceErrorInfo)} ----------");
remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService<IJsonSerializer>().Serialize(remoteServiceErrorInfo, indented: true)); remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService<IJsonSerializer>().Serialize(remoteServiceErrorInfo, indented: true));
var logger = context.GetService<ILogger<AbpExceptionPageFilter>>(NullLogger<AbpExceptionPageFilter>.Instance)!; if (context.Exception != null && exceptionHandlingOptions.ShouldLogException(context.Exception))
logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); {
var logger = context.GetService<ILogger<AbpExceptionPageFilter>>(NullLogger<AbpExceptionPageFilter>.Instance)!;
logger.LogException(context.Exception!, logLevel); logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString());
logger.LogException(context.Exception!, logLevel);
}
await context.GetRequiredService<IExceptionNotifier>().NotifyAsync(new ExceptionNotificationContext(context.Exception!)); await context.GetRequiredService<IExceptionNotifier>().NotifyAsync(new ExceptionNotificationContext(context.Exception!));
@ -113,6 +115,7 @@ public class AbpExceptionPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDe
} }
else else
{ {
var logger = context.GetService<ILogger<AbpExceptionPageFilter>>(NullLogger<AbpExceptionPageFilter>.Instance)!;
logger.LogWarning("HTTP response has already started, cannot set headers and status code!"); logger.LogWarning("HTTP response has already started, cannot set headers and status code!");
} }

8
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) 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 await httpContext
.RequestServices .RequestServices
@ -77,7 +82,6 @@ public class AbpExceptionHandlingMiddleware : AbpMiddlewareBase, ITransientDepen
var errorInfoConverter = httpContext.RequestServices.GetRequiredService<IExceptionToErrorInfoConverter>(); var errorInfoConverter = httpContext.RequestServices.GetRequiredService<IExceptionToErrorInfoConverter>();
var statusCodeFinder = httpContext.RequestServices.GetRequiredService<IHttpExceptionStatusCodeFinder>(); var statusCodeFinder = httpContext.RequestServices.GetRequiredService<IHttpExceptionStatusCodeFinder>();
var jsonSerializer = httpContext.RequestServices.GetRequiredService<IJsonSerializer>(); var jsonSerializer = httpContext.RequestServices.GetRequiredService<IJsonSerializer>();
var exceptionHandlingOptions = httpContext.RequestServices.GetRequiredService<IOptions<AbpExceptionHandlingOptions>>().Value;
httpContext.Response.Clear(); httpContext.Response.Clear();
httpContext.Response.StatusCode = (int)statusCodeFinder.GetStatusCode(httpContext, exception); httpContext.Response.StatusCode = (int)statusCodeFinder.GetStatusCode(httpContext, exception);

13
framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Volo.Abp.AspNetCore.ExceptionHandling; namespace Volo.Abp.AspNetCore.ExceptionHandling;
@ -11,6 +12,12 @@ public class AbpExceptionHandlingOptions
public List<Type> SendExceptionDataToClientTypes { get; set; } 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() public AbpExceptionHandlingOptions()
{ {
SendExceptionsDetailsToClients = false; SendExceptionsDetailsToClients = false;
@ -19,5 +26,11 @@ public class AbpExceptionHandlingOptions
[ [
typeof(IBusinessException) typeof(IBusinessException)
]; ];
ExcludeExceptionFromLoggerSelectors = new List<Func<Exception, bool>>();
}
public bool ShouldLogException(Exception exception)
{
return ExcludeExceptionFromLoggerSelectors.All(selector => !selector(exception));
} }
} }

Loading…
Cancel
Save