forked from microsoft/ApplicationInsights-aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationInsightsInitializer.cs
More file actions
103 lines (91 loc) · 3.4 KB
/
ApplicationInsightsInitializer.cs
File metadata and controls
103 lines (91 loc) · 3.4 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
namespace Microsoft.ApplicationInsights.AspNetCore
{
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Diagnostics;
using Extensions;
using Microsoft.ApplicationInsights.AspNetCore.DiagnosticListeners;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Threading;
/// <summary>
/// Class used to initialize Application Insight diagnostic listeners.
/// </summary>
internal class ApplicationInsightsInitializer : IObserver<DiagnosticListener>, IDisposable
{
private ConcurrentBag<IDisposable> subscriptions;
private readonly IEnumerable<IApplicationInsightDiagnosticListener> diagnosticListeners;
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationInsightsInitializer"/> class.
/// </summary>
public ApplicationInsightsInitializer(
IOptions<ApplicationInsightsServiceOptions> options,
IEnumerable<IApplicationInsightDiagnosticListener> diagnosticListeners,
ILoggerFactory loggerFactory,
IServiceProvider serviceProvider)
{
this.diagnosticListeners = diagnosticListeners;
this.subscriptions = new ConcurrentBag<IDisposable>();
// Add default logger factory for debug mode only if enabled and instrumentation key not set
if (options.Value.EnableDebugLogger && string.IsNullOrEmpty(options.Value.InstrumentationKey))
{
// Do not use extension method here or it will disable debug logger we currently adding
var enableDebugLogger = true;
loggerFactory.AddApplicationInsights(serviceProvider, (s, level) => enableDebugLogger && Debugger.IsAttached, () => enableDebugLogger = false);
}
}
/// <summary>
/// Subscribes diagnostic listeners to sources
/// </summary>
public void Start()
{
this.subscriptions.Add(DiagnosticListener.AllListeners.Subscribe(this));
}
/// <inheritdoc />
void IObserver<DiagnosticListener>.OnNext(DiagnosticListener value)
{
var subs = Volatile.Read(ref this.subscriptions);
if (subs is null)
{
return;
}
foreach (var applicationInsightDiagnosticListener in this.diagnosticListeners)
{
if (applicationInsightDiagnosticListener.ListenerName == value.Name)
{
subs.Add(value.SubscribeWithAdapter(applicationInsightDiagnosticListener));
}
}
}
/// <inheritdoc />
void IObserver<DiagnosticListener>.OnError(Exception error)
{
}
/// <inheritdoc />
void IObserver<DiagnosticListener>.OnCompleted()
{
}
/// <inheritdoc />
public void Dispose()
{
this.Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing)
{
return;
}
var subs = Interlocked.Exchange(ref this.subscriptions, null);
if (subs is null)
{
return;
}
foreach (var subscription in subs)
{
subscription.Dispose();
}
}
}
}