Skip to content

Add ExceptionHandlerOptions.SuppressLoggingCallback #59075

@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 <see cref="HttpContext"/> of the current request. + /// </summary> + public required HttpContext HttpContext { get; init; } + + /// <summary> + /// Gets the result of the exception handler middleware. + /// </summary> + public required ExceptionHandledType ExceptionHandledBy { get; init; } +} +/// <summary> +/// The result of executing <see cref="ExceptionHandlerMiddleware"/>. +/// </summary> +public enum ExceptionHandledType +{ + /// <summary> + /// Exception was unhandled. + /// </summary> + Unhandled, + /// <summary> + /// Exception was handled by an <see cref="Diagnostics.IExceptionHandler"/> instance registered in the DI container. + /// </summary> + ExceptionHandlerService, + /// <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> + ExceptionHandlerCallback, + /// <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.ExceptionHandledBy == ExceptionHandledType.Unhandled; });
app.UseExceptionHandler(new ExceptionHandlerOptions { SuppressLoggingCallback = context => context.ExceptionHandledBy == ExceptionHandledType.ExceptionHandlerService; });

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-approvedAPI was approved in API review, it can be implementedarea-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions