-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Conversation
Product changes ready to review. Tests coming. |
API review: #59075 |
src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerOptions.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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 toSuppressLoggingForHandledExceptions
.
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.
4b877a4
to
ec717c3
Compare
ec717c3
to
dc0a76e
Compare
dc0a76e
to
ca1f983
Compare
API review feedback applied. Please do a final review, and hopefully I can check this in 🙏 |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs
Outdated
Show resolved
Hide resolved
src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerOptions.cs
Show resolved
Hide resolved
src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerOptions.cs
Outdated
Show resolved
Hide resolved
ca1f983
to
e7cf4c9
Compare
@halter73 I applied all your feedback and changed default behavior. Please take another look. |
src/Middleware/Diagnostics/test/UnitTests/ExceptionHandlerMiddlewareTest.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerOptions.cs
Outdated
Show resolved
Hide resolved
src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerOptions.cs
Outdated
Show resolved
Hide resolved
/// <summary> | ||
/// Exception was handled by by <see cref="Builder.ExceptionHandlerOptions.ExceptionHandler"/>. | ||
/// </summary> | ||
ExceptionHandlerDelegate, |
There was a problem hiding this comment.
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
Fixes #54554
Background
Exception handler middleware handles exceptions thrown in the app. There are various mechanisms for handling an exception:
IExceptionHandler
instances. Return true to indicate exception is handled.IProblemDetailsService
instance. Return true to indicate exception is handled.ExceptionHandlerOptions.ExceptionHandler
delegate.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 anError
level, and customers who automatically log allError
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 levelUnhandledException
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.