Open
Description
Background and Motivation
A config callback used to optionally suppress logging by ExceptionHandlerMiddleware
.
For #54554
Proposed API
namespace Microsoft.AspNetCore.Builder;
public class ExceptionHandlerOptions
{
+ /// <summary>
+ /// Gets or sets a callback that can be used to suppress logging by <see cref="ExceptionHandlerMiddleware" />.
+ /// This callback is only run if the exception was handled by the middleware.
+ /// Unhandled exceptions and exceptions thrown after the response has started are always logged.
+ /// </summary>
+ public Func<ExceptionHandlerSuppressLoggingContext, bool>? SuppressLoggingCallback { get; set; }
}
namespace Microsoft.AspNetCore.Diagnostics;
+/// <summary>
+/// The context used to determine whether exception handler middleware should log an exception.
+/// </summary>
+public sealed class ExceptionHandlerSuppressLoggingContext
+{
+ /// <summary>
+ /// Gets the <see cref="System.Exception"/> that the exception handler middleware is processing.
+ /// </summary>
+ public required Exception Exception { get; init; }
+
+ /// <summary>
+ /// Gets the result of the exception handler middleware.
+ /// </summary>
+ public required ExceptionHandlerResult HandlerResult { get; init; }
+}
+/// <summary>
+/// The result of executing <see cref="ExceptionHandlerMiddleware"/>.
+/// </summary>
+public enum ExceptionHandlerResult
+{
+ /// <summary>
+ /// Exception was unhandled.
+ /// </summary>
+ Unhandled,
+ /// <summary>
+ /// Exception was handled by an <see cref="Diagnostics.IExceptionHandler"/> instance registered in the DI container.
+ /// </summary>
+ IExceptionHandler,
+ /// <summary>
+ /// Exception was handled by an <see cref="Http.IProblemDetailsService"/> instance registered in the DI container.
+ /// </summary>
+ ProblemDetailsService,
+ /// <summary>
+ /// Exception was handled by by <see cref="Builder.ExceptionHandlerOptions.ExceptionHandler"/>.
+ /// </summary>
+ ExceptionHandler,
+ /// <summary>
+ /// Exception was handled by by <see cref="Builder.ExceptionHandlerOptions.ExceptionHandlingPath"/>.
+ /// </summary>
+ ExceptionHandlingPath
+}
Defaults to null.
Usage Examples
app.UseExceptionHandler(new ExceptionHandlerOptions
{
SuppressLoggingCallback = context => context.HandlerResult == ExceptionHandlerResult.IExceptionHandler;
});
Alternative Designs
- Could have bool properties to suppress different scenarios. Not flexible
- Could avoid context type on callback. Added to make it easy to pass new properties in the future