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

Merged
merged 11 commits into from
Jul 10, 2025

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
@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 Jun 3, 2025
@JamesNK JamesNK force-pushed the jamesnk/exceptionhandler-logging branch from ec717c3 to dc0a76e Compare July 1, 2025 06:06
@JamesNK JamesNK removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jul 4, 2025
@JamesNK JamesNK force-pushed the jamesnk/exceptionhandler-logging branch from dc0a76e to ca1f983 Compare July 4, 2025 03:08
@JamesNK JamesNK requested a review from halter73 July 4, 2025 03:10
@JamesNK
Copy link
Member Author

JamesNK commented Jul 4, 2025

API review feedback applied.

Please do a final review, and hopefully I can check this in 🙏

@JamesNK

This comment was marked as outdated.

This comment was marked as outdated.

@JamesNK

This comment was marked as outdated.

This comment was marked as outdated.

@JamesNK JamesNK force-pushed the jamesnk/exceptionhandler-logging branch from ca1f983 to e7cf4c9 Compare July 8, 2025 06:53
@JamesNK
Copy link
Member Author

JamesNK commented Jul 8, 2025

@halter73 I applied all your feedback and changed default behavior. Please take another look.

Copy link
Member

@halter73 halter73 left a comment

Choose a reason for hiding this comment

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

Thanks. I'm a big fan of the "Logging" to "Diagnostics" renaming.

/// <summary>
/// Exception was handled by by <see cref="Builder.ExceptionHandlerOptions.ExceptionHandler"/>.
/// </summary>
ExceptionHandlerDelegate,
Copy link
Member Author

Choose a reason for hiding this comment

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

Late change from ExceptionHandlerCallback to ExceptionHandlerDelegate

@JamesNK JamesNK enabled auto-merge (squash) July 10, 2025 01:40
@JamesNK JamesNK merged commit 9757966 into main Jul 10, 2025
26 of 28 checks passed
@JamesNK JamesNK deleted the jamesnk/exceptionhandler-logging branch July 10, 2025 02:04
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview7 milestone Jul 10, 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
3 participants