forked from gzepeda/ApplicationInsights-aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExceptionTrackingMiddleware.cs
43 lines (39 loc) · 1.48 KB
/
ExceptionTrackingMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
namespace Microsoft.ApplicationInsights.AspNetCore
{
using System;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.AspNetCore.Http;
/// <summary>
/// Sends telemetry about exceptions thrown by the application to the Microsoft Application Insights service.
/// </summary>
[Obsolete]
public sealed class ExceptionTrackingMiddleware
{
private readonly RequestDelegate next;
private readonly TelemetryClient telemetryClient;
private readonly string sdkVersion;
public ExceptionTrackingMiddleware(RequestDelegate next, TelemetryClient client)
{
this.next = next;
this.telemetryClient = client;
this.sdkVersion = SdkVersionUtils.VersionPrefix + SdkVersionUtils.GetAssemblyVersion();
}
public async Task Invoke(HttpContext httpContext)
{
try
{
await this.next.Invoke(httpContext);
}
catch (Exception exception)
{
var exceptionTelemetry = new ExceptionTelemetry(exception);
exceptionTelemetry.HandledAt = ExceptionHandledAt.Platform;
exceptionTelemetry.Context.GetInternalContext().SdkVersion = this.sdkVersion;
this.telemetryClient.Track(exceptionTelemetry);
throw;
}
}
}
}