skip to content

After moving an ASP.NET Core API from .NET 8 to .NET 10, exceptions your IExceptionHandler handles no longer appear in the error logs — why, and how do you restore them?

level: seniorimportance: should knowfreq 38%

answer

  1. a .NET 10 default, not a bug
  2. ExceptionHandlerOptions.SuppressDiagnosticsCallback
  3. ExceptionHandledType.ExceptionHandlerService
  4. log, diagnostic event and error.type tag
  5. unhandled and started responses still logged

basics

~10 s

ASP.NET Core 10 stopped writing diagnostics for exceptions an IExceptionHandler handled. Set ExceptionHandlerOptions.SuppressDiagnosticsCallback to return false, or to return true only for expected exceptions, to get the error logs back.

solid answer

~30 s

In ASP.NET Core 10 the exception handler middleware gained `ExceptionHandlerOptions.SuppressDiagnosticsCallback`, and its default changed: when the callback is null, diagnostics are suppressed whenever an `IExceptionHandler` returned true. Suppressed means no `UnhandledException` error log, no `Microsoft.AspNetCore.Diagnostics.HandledException` diagnostic event and no `error.type` tag on `http.server.request.duration`. Unhandled exceptions, exceptions after the response started, and exceptions handled by an error path, delegate or the ProblemDetails fallback are still logged. To restore .NET 8 behaviour, configure `builder.Services.AddExceptionHandler(o => o.SuppressDiagnosticsCallback = _ => false)` and keep `app.UseExceptionHandler()`. A better variant suppresses only expected exceptions by inspecting `ExceptionHandledBy` and `Exception` on the callback's context.

code

csharp · 12 lines
csharp
builder.Services.AddProblemDetails();
builder.Services.AddExceptionHandler<BookNotFoundExceptionHandler>();
builder.Services.AddExceptionHandler(options =>
{
    // Back to .NET 8 logging, except for the expected not-found lookups.
    options.SuppressDiagnosticsCallback = context =>
        context.ExceptionHandledBy == ExceptionHandledType.ExceptionHandlerService
        && context.Exception is BookNotFoundException;
});

var app = builder.Build();
app.UseExceptionHandler(); // reads the options configured above

go deeper

for a junior

Know that ASP.NET Core 10 no longer logs exceptions an IExceptionHandler handled, and that an option named SuppressDiagnosticsCallback controls it.

for a middle

Explain what suppression drops (log, diagnostic event, error.type tag), which cases are always logged, and that the callback returns true to suppress.

for a senior

Check alerting after an upgrade, suppress only expected exceptions via ExceptionHandledBy and Exception, and avoid the options-instance trap that ignores DI-configured options.

for a principal

Decide which failures count as errors for alerting and which are expected outcomes, and make that rule the same for every service after the LTS upgrade.

## What changed in ASP.NET Core 10 Up to ASP.NET Core 9 the exception handler middleware logged **every** exception it handled at Error level, even when an `IExceptionHandler` turned it into a tidy 404 or 409. Teams that throw for expected outcomes, such as a book that does not exist, got error-level noise and false alerts. ASP.NET Core 10 changed the default and added a switch: - `ExceptionHandlerOptions.SuppressDiagnosticsCallback`, of type `Func<ExceptionHandlerSuppressDiagnosticsContext, bool>?`, decides whether to suppress diagnostics for an exception the middleware handled. - When the callback is **null** (the default), the middleware suppresses diagnostics **only** when the exception was handled by an `IExceptionHandler` service, that is, when `ExceptionHandledBy` is `ExceptionHandledType.ExceptionHandlerService`. The release notes list this as a breaking change. An upgrade from .NET 8 that relied on the middleware's log line for alerting loses those entries silently. ## What is suppressed and what is not When diagnostics are suppressed the middleware skips three things: 1. the `UnhandledException` log entry ("An unhandled exception has occurred while executing the request.") from the `ExceptionHandlerMiddleware` category; 2. the `Microsoft.AspNetCore.Diagnostics.HandledException` diagnostic event, which some monitoring agents listen to; 3. the `error.type` tag on the `http.server.request.duration` metric. It still records the `aspnetcore.diagnostics.exceptions` counter, tagged with the handled result and the handler's type name. The callback plays no part, and diagnostics are always written, when: - the response had already started, so the middleware logs and rethrows; - the handler itself threw; - the error path left an empty 404 and the middleware rethrows a wrapped exception. | Handled by | `ExceptionHandledBy` value | Logged by default in 10? | |---|---|---| | an `IExceptionHandler` returning true | `ExceptionHandlerService` | no | | the `IProblemDetailsService` fallback | `ProblemDetailsService` | yes | | an `ExceptionHandler` delegate | `ExceptionHandlerDelegate` | yes | | an `ExceptionHandlingPath` endpoint | `ExceptionHandlingPath` | yes | ## Restoring the logs The callback returns **true to suppress** and **false to keep** diagnostics. To get .NET 8 behaviour back: - `builder.Services.AddExceptionHandler(options => options.SuppressDiagnosticsCallback = _ => false);` and keep `app.UseExceptionHandler()`. The ASP.NET Core 10 release-notes sample writes `SuppressDiagnosticsCallback = context => false;` inside an object initializer; that stray semicolon does not compile. Use a comma or nothing. One trap: `app.UseExceptionHandler(new ExceptionHandlerOptions { ... })` builds the middleware from **that instance only**. Options configured through `AddExceptionHandler(options => ...)` are read only by the overloads that take no options object (`UseExceptionHandler()`). Configure everything in one place. ## Checking an upgrade from .NET 8 1. List the alerts and saved queries that match the middleware's entry: event id 1, event name `UnhandledException`, category `Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware`. 2. For each `IExceptionHandler`, decide whether the exceptions it handles are expected outcomes or faults. 3. Encode that decision in `SuppressDiagnosticsCallback`, or log inside the handlers, before the upgrade ships. 4. Trigger one handled fault in a test environment and confirm the alert still fires. ## A better policy than all or nothing The context gives you `HttpContext`, `Exception` and `ExceptionHandledBy`, so you can suppress only what is expected: - suppress `BookNotFoundException` handled by your not-found handler, because it is a normal outcome; - keep diagnostics for everything else a catch-all handler swallowed, because that is a real fault. Alternatively, leave the default and log inside each handler with an injected `ILogger<T>` at the level that fits (Information for a not-found, Error for a fault). Either way, check after the upgrade that your alert rules still see what they used to. ## Summary - The .NET 10 default hides exceptions an `IExceptionHandler` handled. - `SuppressDiagnosticsCallback` returning false brings them back; returning true selectively keeps the noise down. - Unhandled exceptions and failures after the response started are always logged.

  • Why might SuppressDiagnosticsCallback seem to have no effect after you pass new ExceptionHandlerOptions to UseExceptionHandler?
    `UseExceptionHandler(ExceptionHandlerOptions)` wraps that instance with `Options.Create` and ignores options configured through `AddExceptionHandler(options => ...)`. If the callback was set in DI but the pipeline passes its own options object without it, the default (suppress for `IExceptionHandler`) applies. Set the callback on the object you pass, or call the parameterless `UseExceptionHandler()`.
  • Does the .NET 10 change stop the middleware from recording metrics for handled exceptions?
    Only partly. Suppression drops the `error.type` tag the middleware would add to `http.server.request.duration`, together with the log entry and the diagnostic event. The `aspnetcore.diagnostics.exceptions` counter is still incremented for a handled exception, tagged with the exception type, the result and the handler type, so dashboards can count handled exceptions without logs.

  • In .NET 10 the middleware stops logging every exception, unhandled ones included.
  • Setting SuppressDiagnosticsCallback to return true restores the .NET 8 logging.
  • Exceptions handled by an ExceptionHandlingPath error page are also unlogged by default in .NET 10.
  • Passing new ExceptionHandlerOptions to UseExceptionHandler still merges options set with AddExceptionHandler.
  • The handled exceptions vanished because .NET 10 no longer calls IExceptionHandler at all.

Assumes ASP.NET Core 10, which added SuppressDiagnosticsCallback and stopped logging exceptions an IExceptionHandler handled; ASP.NET Core 8 and 9 logged them.