-
Notifications
You must be signed in to change notification settings - Fork 285
/
ApplicationInsightsExtensions.cs
182 lines (164 loc) · 8.68 KB
/
ApplicationInsightsExtensions.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
namespace Microsoft.Extensions.DependencyInjection
{
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.ApplicationInsights.AspNetCore;
using Microsoft.ApplicationInsights.AspNetCore.Extensibility.Implementation.Tracing;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers;
using Microsoft.ApplicationInsights.Extensibility;
#if NETSTANDARD2_0
using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector;
#endif
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Shared.Implementation;
/// <summary>
/// Extension methods for <see cref="IServiceCollection"/> that allow adding Application Insights services to application.
/// </summary>
public static partial class ApplicationInsightsExtensions
{
#pragma warning disable CS1591 // Missing XML comment.
[SuppressMessage(category: "StyleCop Documentation Rules", checkId: "SA1600:ElementsMustBeDocumented", Justification = "Obsolete method.")]
[Obsolete("This middleware is no longer needed. Enable Request monitoring using services.AddApplicationInsights")]
public static IApplicationBuilder UseApplicationInsightsRequestTelemetry(this IApplicationBuilder app)
{
return app;
}
[SuppressMessage(category: "StyleCop Documentation Rules", checkId: "SA1600:ElementsMustBeDocumented", Justification = "Obsolete method.")]
[Obsolete("This middleware is no longer needed to track exceptions as they are automatically tracked by RequestTrackingTelemetryModule")]
public static IApplicationBuilder UseApplicationInsightsExceptionTelemetry(this IApplicationBuilder app)
{
return app.UseMiddleware<ExceptionTrackingMiddleware>();
}
#pragma warning restore CS1591 // Missing XML comment.
/// <summary>
/// Adds Application Insights services into service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> instance.</param>
/// <param name="instrumentationKey">Instrumentation key to use for telemetry.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddApplicationInsightsTelemetry(
this IServiceCollection services,
string instrumentationKey)
{
services.AddApplicationInsightsTelemetry(options => options.InstrumentationKey = instrumentationKey);
return services;
}
/// <summary>
/// Adds Application Insights services into service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> instance.</param>
/// <param name="configuration">Configuration to use for sending telemetry.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddApplicationInsightsTelemetry(
this IServiceCollection services,
IConfiguration configuration)
{
services.AddApplicationInsightsTelemetry(options => AddTelemetryConfiguration(configuration, options));
return services;
}
/// <summary>
/// Adds Application Insights services into service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> instance.</param>
/// <param name="options">The action used to configure the options.</param>
/// <returns>
/// The <see cref="IServiceCollection"/>.
/// </returns>
public static IServiceCollection AddApplicationInsightsTelemetry(
this IServiceCollection services,
Action<ApplicationInsightsServiceOptions> options)
{
services.AddApplicationInsightsTelemetry();
services.Configure(options);
return services;
}
/// <summary>
/// Adds Application Insights services into service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> instance.</param>
/// <param name="options">The options instance used to configure with.</param>
/// <returns>
/// The <see cref="IServiceCollection"/>.
/// </returns>
public static IServiceCollection AddApplicationInsightsTelemetry(
this IServiceCollection services,
ApplicationInsightsServiceOptions options)
{
services.AddApplicationInsightsTelemetry();
services.Configure((ApplicationInsightsServiceOptions o) => options.CopyPropertiesTo(o));
return services;
}
/// <summary>
/// Adds Application Insights services into service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> instance.</param>
/// <returns>
/// The <see cref="IServiceCollection"/>.
/// </returns>
public static IServiceCollection AddApplicationInsightsTelemetry(this IServiceCollection services)
{
try
{
if (!IsApplicationInsightsAdded(services))
{
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
AddAspNetCoreWebTelemetryInitializers(services);
AddCommonInitializers(services);
// Request Tracking.
services.AddSingleton<ITelemetryModule, RequestTrackingTelemetryModule>();
services.ConfigureTelemetryModule<RequestTrackingTelemetryModule>((module, options) =>
{
if (options.EnableRequestTrackingTelemetryModule)
{
module.CollectionOptions = options.RequestCollectionOptions;
}
});
AddCommonTelemetryModules(services);
AddTelemetryChannel(services);
#if NETSTANDARD2_0
ConfigureEventCounterModuleWithSystemCounters(services);
#endif
services.TryAddSingleton<IConfigureOptions<ApplicationInsightsServiceOptions>,
DefaultApplicationInsightsServiceConfigureOptions>();
AddTelemetryConfigAndClient(services);
AddDefaultApplicationIdProvider(services);
// Using startup filter instead of starting DiagnosticListeners directly because
// AspNetCoreHostingDiagnosticListener injects TelemetryClient that injects TelemetryConfiguration
// that requires IOptions infrastructure to run and initialize
services.AddSingleton<IStartupFilter, ApplicationInsightsStartupFilter>();
services.AddSingleton<IJavaScriptSnippet, JavaScriptSnippet>();
// Add 'JavaScriptSnippet' "Service" for backwards compatibility. To remove in favour of 'IJavaScriptSnippet'.
services.AddSingleton<JavaScriptSnippet>();
// NetStandard2.0 has a package reference to Microsoft.Extensions.Logging.ApplicationInsights, and
// enables ApplicationInsightsLoggerProvider by default.
#if NETSTANDARD2_0 || NET461
AddApplicationInsightsLoggerProvider(services);
#endif
}
return services;
}
catch (Exception e)
{
AspNetCoreEventSource.Instance.LogError(e.ToInvariantString());
return services;
}
}
private static void AddAspNetCoreWebTelemetryInitializers(IServiceCollection services)
{
services.AddSingleton<ITelemetryInitializer, AzureAppServiceRoleNameFromHostNameHeaderInitializer>();
services.AddSingleton<ITelemetryInitializer, ClientIpHeaderTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, OperationNameTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, SyntheticTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, WebSessionTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, WebUserTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, AspNetCoreEnvironmentTelemetryInitializer>();
}
}
}