Skip "Handled" error #4448
-
Trying to prevent Sentry sending errors that are handled in a try catch try
{
..... throws exception
}
catch (Exception ex)
{
ILogger.LogException(ex, "MyHandledException")
} will something like below work? // Add this to the SDK initialization callback
options.SetBeforeSend((sentryEvent, hint) =>
{
if (HasTerminalException(sentryEvent.Exception, sentryEvent.SentryExceptions))
{
System.Diagnostics.Trace.WriteLine(
$"[TRACE]: Sentry event is being sent: {sentryEvent.EventId} - {sentryEvent.Message}");
return sentryEvent;
}
System.Diagnostics.Trace.WriteLine(
$"[TRACE]: Sentry event is being dropped: {sentryEvent.EventId} - {sentryEvent.Message}");
return null;
bool HasTerminalException(Exception? ex, IEnumerable<SentryException>? ses)
{
// The exception is considered terminal if it is marked unhandled,
// UNLESS it comes from the UnobservedTaskExceptionIntegration
if (ex?.Data[Mechanism.HandledKey] is false)
{
return ex.Data[Mechanism.MechanismKey] as string != "UnobservedTaskException";
}
return ses?.Any(e => e.Mechanism is { Handled: false } mechanism &&
mechanism.Type != "UnobservedTaskException") ?? false;
}
}); Or there is a better way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
TLDR;From what I can tell, you never want Exceptions that get logged via Long explanation
Exceptions that are captured from your own code, via I think you could check for unhandled exceptions with something like: sentryEvent.SentryExceptions?.Any(e => e.Mechanism is { Handled: false }) ?? false; However filtering all handled exceptions would prevent something like the following from being captured: try {
throw new Exception();
}
catch (Exception e) {
SentrySdk.CaptureException(e);
} I don't think you want to do that. |
Beta Was this translation helpful? Give feedback.
@Syed-RI
TLDR;
From what I can tell, you never want Exceptions that get logged via
ILogger
to be turned into events that get to Sentry. The easiest way to do that is just to setoptions.MinimumEventLevel = LogLevel.None
when initialising Sentry... if you do that, nothing you log via theILogger
interface will be captured as an Event in Sentry.Long explanation
Mechanism.Handled
is generally only set tofalse
for exceptions that you don't catch and which the Sentry SDK catches automatically (e.g. via the AppDomainUnhandledExceptionIntegration).Exceptions that are captured from your own code, via
SentrySdk.CaptureException
orILogger
(where the log level is higher than SentryLoggingOption.…