dotnet / dotnet/AspNetCore.Docs
The section about IExceptionHandler is misleading.
- Dominant language
- C#
- Stars
- 13.1k
- Forks
- 24.6k
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 97
Description
### Description
The article creates an impression that it's enough to add custom implementation of `IExceptionHandler`. However, I had to call `UseExceptionHanlder` middleware.
This [comment](https://github.com/dotnet/aspnetcore/issues/52622#issuecomment-1845720770) is a confirmation that the former does not work without the latter.
However, there are more surprises:
If I call:
```
app.UseExceptionHandler();
```
I get the following exception:
> System.InvalidOperationException: 'An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' property must be set in 'UseExceptionHandler()'. Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows: 'services.AddExceptionHandler(options => { ... });' or configure to generate a 'ProblemDetails' response in 'service.AddProblemDetails()'.'
If I call it like this:
```
app.UseExceptionHandler(c => {});
```
custom exception handler is called, but I see two log entries about the error despite my handler returns `true`:
> "Category": "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware",
and then from my handler:
> "Category": "ExceptionHandler"
I'd appreciate if this WTF behavior was properly explained.
> the 'ExceptionHandler' property must be set in 'UseExceptionHandler()'.
Why do I need to call it, if I called `builder.Services.AddExceptionHandler();`
Here's the code I use:
```CSharp
using Microsoft.AspNetCore.Diagnostics;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddExceptionHandler();
builder.Services.AddControllers();
builder.Logging.AddJsonConsole(c =>
{
c.UseUtcTimestamp = true;
c.IncludeScopes = true;
c.JsonWriterOptions = new()
{
Indented = true,
};
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseExceptionHandler(c => {});
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
internal sealed class ExceptionHandler : IExceptionHandler
{
private readonly ILogger _logger;
public ExceptionHandler(ILogger logger)
{
_logger = logger;
}
public async ValueTask TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
{
_logger.LogError(exception, "You will see it twice");
return true;
}
}
```
### Page URL
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-8.0
### Content source URL
https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/error-handling.md
### Document ID
38515dfb-91a5-b395-db9d-084bbaf095c8
### Article author
@tdykstra
---
[Associated WorkItem - 506180](https://dev.azure.com/msft-skilling/Content/_workitems/edit/506180)
Contributor guide
Assessment
This issue has not been assessed yet.