Skip to content

Commit d4bffc7

Browse files
Add isHandled=true for handled exceptions (#2626)
Have isHandled=true for handled exceptions Have isHandled=false for unhandled exceptions Issue: #2312
1 parent 09ded4c commit d4bffc7

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

src/integrations/Elastic.Apm.AspNetCore/DiagnosticListener/AspNetCoreDiagnosticListener.cs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,37 +74,33 @@ protected override void HandleOnNext(KeyValuePair<string, object> kv)
7474
}
7575
switch (kv.Key)
7676
{
77-
case "Microsoft.AspNetCore.Diagnostics.UnhandledException": //Called when exception handler is registered
77+
case "Microsoft.AspNetCore.Diagnostics.UnhandledException":
78+
HandleException(_defaultHttpContextFetcher, _exceptionContextPropertyFetcher, kv, false);
79+
break;
7880
case "Microsoft.AspNetCore.Diagnostics.HandledException":
79-
if (!(_defaultHttpContextFetcher.Fetch(kv.Value) is DefaultHttpContext httpContextDiagnosticsUnhandledException))
80-
return;
81-
if (!(_exceptionContextPropertyFetcher.Fetch(kv.Value) is Exception diagnosticsException))
82-
return;
83-
if (!ProcessingRequests.TryGetValue(httpContextDiagnosticsUnhandledException, out var iDiagnosticsTransaction))
84-
return;
85-
86-
if (iDiagnosticsTransaction is Transaction diagnosticsTransaction)
87-
{
88-
diagnosticsTransaction.CollectRequestBody(true, new AspNetCoreHttpRequest(httpContextDiagnosticsUnhandledException.Request), Logger);
89-
diagnosticsTransaction.CaptureException(diagnosticsException);
90-
}
91-
81+
HandleException(_defaultHttpContextFetcher, _exceptionContextPropertyFetcher, kv, true);
9282
break;
9383
case "Microsoft.AspNetCore.Hosting.UnhandledException": // Not called when exception handler registered
94-
if (!(_hostDefaultHttpContextFetcher.Fetch(kv.Value) is DefaultHttpContext httpContextUnhandledException))
95-
return;
96-
if (!(_hostExceptionContextPropertyFetcher.Fetch(kv.Value) is Exception exception))
97-
return;
98-
if (!ProcessingRequests.TryGetValue(httpContextUnhandledException, out var iCurrentTransaction))
99-
return;
100-
101-
if (iCurrentTransaction is Transaction currentTransaction)
102-
{
103-
currentTransaction.CollectRequestBody(true, new AspNetCoreHttpRequest(httpContextUnhandledException.Request), Logger);
104-
currentTransaction.CaptureException(exception);
105-
}
84+
HandleException(_hostDefaultHttpContextFetcher, _hostExceptionContextPropertyFetcher, kv, false);
10685
break;
10786
}
10887
}
88+
89+
private bool HandleException(PropertyFetcher propertyFetcher, PropertyFetcher exceptionPropertyFetcher, KeyValuePair<string, object> kv, bool isHandled)
90+
{
91+
if (propertyFetcher.Fetch(kv.Value) is not DefaultHttpContext exception)
92+
return false;
93+
if (exceptionPropertyFetcher.Fetch(kv.Value) is not Exception httpContextException)
94+
return false;
95+
if (!ProcessingRequests.TryGetValue(exception, out var iTransaction))
96+
return false;
97+
if (iTransaction is Transaction transaction)
98+
{
99+
transaction.CollectRequestBody(true, new AspNetCoreHttpRequest(exception.Request), Logger);
100+
transaction.CaptureException(httpContextException, isHandled: isHandled);
101+
}
102+
103+
return true;
104+
}
109105
}
110106
}

0 commit comments

Comments
 (0)