Skip to content

Commit f12bf87

Browse files
CopilottdykstraJamesNK
authored
Document new ExceptionHandlerOptions.SuppressDiagnosticsCallback setting (#36157)
* Initial plan * Document SuppressDiagnosticsCallback in error-handling.md Co-authored-by: tdykstra <[email protected]> * Update aspnetcore/fundamentals/error-handling.md * Update aspnetcore/fundamentals/error-handling.md * Update aspnetcore/fundamentals/error-handling.md * Update aspnetcore/fundamentals/error-handling.md * Update aspnetcore/fundamentals/error-handling.md * Address review feedback: clarify middleware text and remove xref link Co-authored-by: JamesNK <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: tdykstra <[email protected]> Co-authored-by: Tom Dykstra <[email protected]> Co-authored-by: JamesNK <[email protected]>
1 parent f980f93 commit f12bf87

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

aspnetcore/fundamentals/error-handling.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
---
22
title: Handle errors in ASP.NET Core
3+
ai-usage: ai-assisted
34
author: tdykstra
45
description: Discover how to handle errors in ASP.NET Core apps.
56
monikerRange: '>= aspnetcore-3.1'
67
ms.author: tdykstra
78
ms.custom: mvc
8-
ms.date: 01/15/2025
9+
ms.date: 09/25/2025
910
uid: fundamentals/error-handling
1011
---
1112
# Handle errors in ASP.NET Core
@@ -79,11 +80,13 @@ Another way to use a lambda is to set the status code based on the exception typ
7980
8081
## IExceptionHandler
8182

82-
[IExceptionHandler](/dotnet/api/microsoft.aspnetcore.diagnostics.iexceptionhandler) is an interface that gives the developer a callback for handling known exceptions in a central location.
83+
[IExceptionHandler](/dotnet/api/microsoft.aspnetcore.diagnostics.iexceptionhandler) is an interface that gives the developer a callback for handling known exceptions in a central location. The interface contains a single method, [`TryHandleAsync`](/dotnet/api/microsoft.aspnetcore.diagnostics.iexceptionhandler.tryhandleasync), which receives an `HttpContext` and an `Exception` parameter.
8384

8485
`IExceptionHandler` implementations are registered by calling [`IServiceCollection.AddExceptionHandler<T>`](/dotnet/api/microsoft.extensions.dependencyinjection.exceptionhandlerservicecollectionextensions.addexceptionhandler). The lifetime of an `IExceptionHandler` instance is singleton. Multiple implementations can be added, and they're called in the order registered.
8586

86-
If an exception handler handles a request, it can return `true` to stop processing. If an exception isn't handled by any exception handler, then control falls back to the default behavior and options from the middleware. Different metrics and logs are emitted for handled versus unhandled exceptions.
87+
Exception handling middleware iterates through registered exception handlers in order until one returns `true` from `TryHandleAsync`, indicating that the exception has been handled. If an exception handler handles an exception, it can return `true` to stop processing. If an exception isn't handled by any exception handler, then control falls back to the default behavior and options from the middleware.
88+
89+
Starting in .NET 10, the default behavior is to suppress emission of diagnostics such as logs and metrics for handled exceptions (when `TryHandleAsync` returns `true`). This differs from earlier versions (.NET 8 and 9) where diagnostics were always emitted regardless of whether the exception was handled. The default behavior can be changed by setting [SuppressDiagnosticsCallback](#suppressdiagnosticscallback).
8790

8891
The following example shows an `IExceptionHandler` implementation:
8992

@@ -103,6 +106,30 @@ In other environments:
103106
* The `CustomExceptionHandler` is called first to handle an exception.
104107
* After logging the exception, the `TryHandleAsync` method returns `false`, so the [`/Error` page](#exception-handler-page) is shown.
105108

109+
### SuppressDiagnosticsCallback
110+
111+
Starting in .NET 10, you can control whether the exception handling middleware writes diagnostics for handled exceptions by configuring the `SuppressDiagnosticsCallback` property on `ExceptionHandlerOptions`. This callback receives the exception context and allows you to determine whether diagnostics should be suppressed based on the specific exception or request.
112+
113+
To revert to the .NET 8 and 9 behavior where diagnostics are always emitted for handled exceptions, set the callback to always return `false`:
114+
115+
```csharp
116+
app.UseExceptionHandler(new ExceptionHandlerOptions
117+
{
118+
SuppressDiagnosticsCallback = context => false
119+
});
120+
```
121+
122+
You can also conditionally suppress diagnostics based on the exception type or other context:
123+
124+
```csharp
125+
app.UseExceptionHandler(new ExceptionHandlerOptions
126+
{
127+
SuppressDiagnosticsCallback = context => context.Exception is ArgumentException
128+
});
129+
```
130+
131+
When an exception isn't handled by any `IExceptionHandler` implementation (all handlers return `false` from `TryHandleAsync`), control falls back to the default behavior and options from the middleware, and diagnostics are emitted according to the middleware's standard behavior.
132+
106133
<!-- links to this in other docs require sestatuscodepages -->
107134
<a name="sestatuscodepages"></a>
108135

0 commit comments

Comments
 (0)