Skip to content

Add ExceptionHandlerOptions.SuppressLoggingCallback #59075

Open
@JamesNK

Description

@JamesNK

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

Risks

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviewsarea-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions