-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathApplicationInsightsExtensions.cs
More file actions
117 lines (109 loc) · 5.46 KB
/
ApplicationInsightsExtensions.cs
File metadata and controls
117 lines (109 loc) · 5.46 KB
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
namespace Microsoft.Extensions.DependencyInjection
{
using System;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
using Microsoft.ApplicationInsights.WindowsServer;
using Microsoft.ApplicationInsights.WorkerService;
using Microsoft.ApplicationInsights.WorkerService.Implementation.Tracing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
/// <summary>
/// Extension methods for <see cref="IServiceCollection"/> that allow adding Application Insights services to application.
/// </summary>
public static partial class ApplicationInsightsExtensions
{
/// <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>
[Obsolete("InstrumentationKey based global ingestion is being deprecated. Use the AddApplicationInsightsTelemetryWorkerService() overload which accepts Action<ApplicationInsightsServiceOptions> and set ApplicationInsightsServiceOptions.ConnectionString. See https://github.com/microsoft/ApplicationInsights-dotnet/issues/2560 for more details.")]
public static IServiceCollection AddApplicationInsightsTelemetryWorkerService(
this IServiceCollection services,
string instrumentationKey)
{
services.AddApplicationInsightsTelemetryWorkerService(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 AddApplicationInsightsTelemetryWorkerService(
this IServiceCollection services,
IConfiguration configuration)
{
services.AddApplicationInsightsTelemetryWorkerService(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 AddApplicationInsightsTelemetryWorkerService(
this IServiceCollection services,
Action<ApplicationInsightsServiceOptions> options)
{
services.AddApplicationInsightsTelemetryWorkerService();
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 AddApplicationInsightsTelemetryWorkerService(
this IServiceCollection services,
ApplicationInsightsServiceOptions options)
{
services.AddApplicationInsightsTelemetryWorkerService();
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 AddApplicationInsightsTelemetryWorkerService(this IServiceCollection services)
{
try
{
if (!IsApplicationInsightsAdded(services))
{
services.AddSingleton<ITelemetryInitializer, AzureWebAppRoleEnvironmentTelemetryInitializer>();
AddCommonInitializers(services);
AddCommonTelemetryModules(services);
AddTelemetryChannel(services);
services
.TryAddSingleton<IConfigureOptions<ApplicationInsightsServiceOptions>,
DefaultApplicationInsightsServiceConfigureOptions>();
AddDefaultApplicationIdProvider(services);
AddTelemetryConfigAndClient(services);
AddApplicationInsightsLoggerProvider(services);
}
return services;
}
catch (Exception e)
{
WorkerServiceEventSource.Instance.LogError(e.ToInvariantString());
return services;
}
}
}
}