Add option to exception handler middleware to not log error #59074
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 new option to the exception handler,
ExceptionHandlerOptions.SuppressLoggingOnHandledException
(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.