Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/platforms/dotnet/common/configuration/filtering.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,48 @@ The SDK supports the <PlatformIdentifier name="before-send" /> callback method.

Note also that breadcrumbs can be filtered, as discussed in [our Breadcrumbs documentation](/product/issues/issue-details/breadcrumbs/).

#### Filtering by Unhandled or Terminal Exceptions

When filtering events in <PlatformIdentifier name="before-send" />, you may want to treat unhandled exceptions differently from handled ones. For example, you might drop noisy exceptions (like network timeouts) if these were handled, but capture them if they crashed your app.

The SDK provides two extension methods on `SentryEvent` to enable this:

- `IsFromUnhandledException()` returns `true` when the event came from an exception that wasn't caught by your application code, but was instead captured through an unhandled exception hook (such as `AppDomain.UnhandledException`, ASP.NET Core middleware, or another integration).
- `IsFromTerminalException()` returns `true` only when the unhandled exception caused the application to crash. Some integrations capture unhandled exceptions that don't terminate the process (for example, unobserved task exceptions) and mark them as non-terminal.

```csharp
options.SetBeforeSend((sentryEvent, hint) =>
{
// Always send unhandled exceptions, even if they'd be filtered below
if (sentryEvent.IsFromUnhandledException())
{
return sentryEvent;
}

// Drop noisy handled exceptions
if (sentryEvent.Exception is HttpRequestException)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The C# code example uses HttpRequestException but is missing the required using System.Net.Http; directive, which will cause compilation errors for some users copying the code.
Severity: LOW

Suggested Fix

Add the using System.Net.Http; directive at the top of the C# code block to ensure the example compiles correctly for all users, regardless of their project's implicit using settings.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: docs/platforms/dotnet/common/configuration/filtering.mdx#L44

Potential issue: The C# code example in the documentation uses `HttpRequestException`
without including the necessary `using System.Net.Http;` directive. While projects using
.NET 6+ with implicit usings enabled will compile this code, it will fail for users on
other platforms like .NET Framework, older .NET Core versions, or Unity. This creates a
poor user experience as copied code will not work out-of-the-box. The corresponding F#
example correctly includes its equivalent import (`open System.Net.Http`), suggesting
this was an unintentional omission.

{
return null; // Don't send this event to Sentry
}

return sentryEvent;
});
```

```fsharp
open Sentry
open System.Net.Http
Comment on lines +54 to +55

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: F# has no implicit usings / opens


options.SetBeforeSend(fun sentryEvent hint ->
Comment thread
jamescrosswell marked this conversation as resolved.
if sentryEvent.IsFromUnhandledException() then
sentryEvent // Always send unhandled exceptions, even if filtered below
elif (sentryEvent.Exception :? HttpRequestException) then
null // Drop noisy handled exceptions
else
sentryEvent
)
```

Comment thread
sentry[bot] marked this conversation as resolved.
### Using <PlatformIdentifier name="add-exception-filter" /> and <PlatformIdentifier name="add-exception-filter-for-type" />

The SDK also allows you to provide your own, custom exception filters. These have to inherit from <PlatformIdentifier name="IExceptionFilter" />
Expand Down
8 changes: 4 additions & 4 deletions platform-includes/configuration/before-send/dotnet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ options.SetBeforeSend((sentryEvent, hint) =>
open Sentry

// Add this to the SDK initialization callback
options.SetBeforeSend(fun event hint ->
if event.Exception <> null && event.Exception.Message.Contains("Noisy Exception") then
options.SetBeforeSend(fun sentryEvent hint ->
if sentryEvent.Exception <> null && sentryEvent.Exception.Message.Contains("Noisy Exception") then
null // Don't send this event to Sentry
else
event.ServerName <- null // Never send Server Name to Sentry
event
sentryEvent.ServerName <- null // Never send Server Name to Sentry
sentryEvent
)
```
Loading