Skip to content

Add option to exception handler middleware to suppress logging #59074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented Nov 20, 2024

Fixes #54554

Background

Exception handler middleware handles exceptions thrown in the app. There are various mechanisms for handling an exception:

  • Registered IExceptionHandler instances. Return true to indicate exception is handled.
  • Registered IProblemDetailsService instance. Return true to indicate exception is handled.
  • Configured ExceptionHandlerOptions.ExceptionHandler delegate.
  • Configured ExceptionHandlerOptions.StatusCodeSelector delegate.

If the exception is handled then the middleware invoke returns without rethrowing the exception. Unhandled exceptions are rethrown back into the middleware pipeline.

Problem

Today the exception handle middleware always writes the UnhandledException log message (skipped if the exception is related to the request being aborted). I think the idea here is the exception handler received an unhandled error. The problem is it is logged at an Error level, and customers who automatically log all Error level logs could get a potentially unwanted error the message.

The request from users is to only write the UnhandledException log message if the exception handler didn't handle the exception. That stops the error level UnhandledException log message from being logged.

Fix

The PR adds a callback to the exception handler options, ExceptionHandlerOptions.SuppressLoggingCallback (final name TBD). It keeps the current behavior but gives an option to opt-in to only logging the unhandled exception log message if the middleware didn't handle the exception.

@JamesNK JamesNK added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Nov 20, 2024
@JamesNK
Copy link
Member Author

JamesNK commented Nov 20, 2024

Product changes ready to review. Tests coming.

@JamesNK
Copy link
Member Author

JamesNK commented Nov 20, 2024

API review: #59075

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Dec 2, 2024
@danmoseley danmoseley requested a review from Copilot February 14, 2025 04:01
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • src/Middleware/Diagnostics/src/PublicAPI.Unshipped.txt: Language not supported
Comments suppressed due to low confidence (3)

src/Middleware/Diagnostics/test/UnitTests/ExceptionHandlerMiddlewareTest.cs:127

  • [nitpick] The class name 'TestHttpResponseFeature' is ambiguous. It should be renamed to indicate its purpose more clearly or documented.
private sealed class TestHttpResponseFeature : HttpResponseFeature

src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerOptions.cs:54

  • [nitpick] The name SuppressLoggingIExceptionHandler is unclear. It should be renamed to SuppressLoggingForHandledExceptions.
public bool SuppressLoggingIExceptionHandler { get; set; }

src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerOptions.cs:43

  • There is a spelling error in the comment. It should be 'HTTP' instead of 'http'.
/// Gets or sets a delegate used to map an exception to a http status code.

@JamesNK JamesNK force-pushed the jamesnk/exceptionhandler-logging branch from 4b877a4 to ec717c3 Compare May 27, 2025 05:35
@JamesNK JamesNK removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label May 27, 2025
@JamesNK JamesNK changed the title Add option to exception handler middleware to not log error Add option to exception handler middleware to suppress logging May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unhandled exceptions are being logged before custom IExceptionHandler is being called
2 participants