Description
Is there an existing issue for this?
- I have searched the existing issues
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 method private 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 the IExceptionHandler
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
- Implement custom
IExceptionHandler
- Register the handler:
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
- Register the middleware: app.UseExceptionHandler();
- Implement an endpoint that throws an exception
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