-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Unhandled exceptions are being logged before custom IExceptionHandler is being called #54554
Comments
This is a confusing behaviour giving the example in the docs. In that example implementation of |
@AlOnestone01 Thanks for reporting this issue! This change in behavior is a result of some work we did in .NET 8 to add support for metrics to ASP.NET Core. The docs that @OskarKlintrot references in his comments haven't been updated to reflect this change in behavior. @JamesNK Assuming this behavior change is intentional, did we author a breaking change notice here? |
Is this a change in behavior? I looked at the PR that added The only difference is Update: Ok, I looked into this more and I guess the request here is if an I'm going to move this into 9.0 milestone. I don't know if there is time, but we should consider addressing this. |
Just to add to this issue: A Diagnostic Event will always be sent by the default ExceptionHandler, regardless of if it is "handled" by an |
I can confirm the problem. I disabled the Log for Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware to mitigate the log issue. The IExceptionHandler should control the logging. The Application Insights topic is also an issue. Depending on the IExceptionHandler outcome the exception should be added to Application Insights instead for all possible exceptions. |
Hi! In my opinion, this is a major problem for the adoption of As mentioned by @JamesNK, changing this behavior in .NET 9 would be nice to only break with .NET 8.
Also, disabling logs from |
It’s too late to do it in 9. We can look at changing it in 10. |
Note if using Serilog rather than Microsoft logger, the configuration for disabling is:
In our case, we wanted to treat some thrown exceptions as client errors and others as server errors, returning 400-499 / 500-599 status codes respectively. Disabling the in-built logging to only log server errors inside of IExceptionHandler was necessary to avoid spamming the logs. One could take the stance that all exceptions should be logged and instead an IActionResult should be explicitly returned for client errors. Our stack made use of various service layers (that have no knowledge of ASP.NET Core) that throw validation errors (that should be treated as client errors). Duplicating this validation logic or adding a try/catch around each of these calls was infeasible. Funnily we actually had the reverse issue of not logging explicitly returned ObjectResult 500-599 status codes. The workaround was adding logging in an IAlwaysRunResultFilter. |
Is there an existing issue for this?
Describe the bug
When using a custom
IExceptionHandler
an unhandled exception is being logged (e.g. to console) before the custom exception handler is being called. The exception is being logged with the message:An unhandled exception has occurred while executing the request
This happens in
ExceptionHandlerMiddlewareImpl.cs
in the methodprivate async Task HandleException(HttpContext context, ExceptionDispatchInfo edi)
:DiagnosticsTelemetry.ReportUnhandledException(_logger, context, edi.SourceException);
After that the custom
IExceptionHandler
is being called.Expected Behavior
If there is a custom
IExceptionHandler
registered, it is being called without logging anything before. In theIExceptionHandler
the developer can decide if and how to log the unhandled exception.Steps To Reproduce
Minimalistic project for reproduction: https://github.com/AlOnestone01/custom-exception-handling
IExceptionHandler
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
Exceptions (if any)
No response
.NET Version
8.0.202
Anything else?
Sample output of the minimalistic project:
exception-log.txt
dotnet --info:
.NET SDK:
Version: 8.0.202
Commit: 25674bb2f4
Workload version: 8.0.200-manifests.8cf8de6d
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.202\
.NET workloads installed:
There are no installed workloads to display.
Host:
Version: 8.0.3
Architecture: x64
Commit: 9f4b1f5d66
.NET SDKs installed:
8.0.202 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
The text was updated successfully, but these errors were encountered: