-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
TraceConfiguration.cs
200 lines (174 loc) · 8.35 KB
/
TraceConfiguration.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.Diagnostics
{
public static class TraceConfiguration
{
private static volatile bool s_registered;
/// <summary>
/// Register the configuration system to apply settings from configuration files
/// to <seealso cref="System.Diagnostics.TraceSource"/> and related classes.
/// </summary>
public static void Register()
{
if (!s_registered)
{
// Registering callbacks more than once is fine, but avoid the overhead common cases without taking a lock.
Trace.Refreshing += RefreshingConfiguration;
Switch.Initializing += InitializingSwitch;
TraceSource.Initializing += InitializingTraceSource;
ConfigureTraceSettings();
s_registered = true;
}
}
private static void RefreshingConfiguration(object sender, EventArgs e) => DiagnosticsConfiguration.Refresh();
private static void InitializingTraceSource(object sender, InitializingTraceSourceEventArgs e)
{
TraceSource traceSource = e.TraceSource;
// Ported from https://referencesource.microsoft.com/#System/compmod/system/diagnostics/TraceSource.cs,176
SourceElementsCollection sources = DiagnosticsConfiguration.Sources;
if (sources != null)
{
SourceElement sourceElement = sources[traceSource.Name];
if (sourceElement != null)
{
e.WasInitialized = true;
// First check if the type changed.
if (HasSourceSwitchTypeChanged())
{
if (!string.IsNullOrEmpty(sourceElement.SwitchName))
{
CreateSwitch(sourceElement.SwitchType, sourceElement.SwitchName);
}
else
{
CreateSwitch(sourceElement.SwitchType, traceSource.Name);
if (!string.IsNullOrEmpty(sourceElement.SwitchValue))
{
traceSource.Switch.Level = (SourceLevels)Enum.Parse(typeof(SourceLevels), sourceElement.SwitchValue);
}
}
}
else if (!string.IsNullOrEmpty(sourceElement.SwitchName))
{
// Create a new switch if the name changed, otherwise just refresh.
if (sourceElement.SwitchName != traceSource.Switch.DisplayName)
CreateSwitch(sourceElement.SwitchType, sourceElement.SwitchName);
else
{
traceSource.Switch.Refresh();
}
}
else
{
// The SwitchValue changed; just update our internalSwitch.
if (!string.IsNullOrEmpty(sourceElement.SwitchValue))
{
traceSource.Switch.Level = (SourceLevels)Enum.Parse(typeof(SourceLevels), sourceElement.SwitchValue);
}
else
{
traceSource.Switch.Level = SourceLevels.Off;
}
}
TraceListener[] newListenerCollection = new TraceListener[sourceElement.Listeners.Count];
int listnerOffset = 0;
foreach (ListenerElement listenerElement in sourceElement.Listeners)
{
TraceListener listener = traceSource.Listeners[listenerElement.Name];
if (listener != null)
{
newListenerCollection[listnerOffset++] = listenerElement.RefreshRuntimeObject(listener);
}
else
{
newListenerCollection[listnerOffset++] = listenerElement.GetRuntimeObject();
}
}
TraceUtils.CopyStringDictionary(sourceElement.Attributes, traceSource.Attributes);
traceSource.Listeners.Clear();
traceSource.Listeners.AddRange(newListenerCollection);
}
else
{
// There was no config, so clear whatever we have.
traceSource.Switch.Level = traceSource.DefaultLevel;
traceSource.Listeners.Clear();
traceSource.Attributes.Clear();
}
bool HasSourceSwitchTypeChanged()
{
string sourceTypeName = sourceElement.SwitchType;
Type currentType = traceSource.Switch.GetType();
if (string.IsNullOrEmpty(sourceTypeName))
{
// SourceSwitch is the default switch type.
return currentType != typeof(SourceSwitch);
}
if (sourceTypeName == currentType.FullName)
{
return false;
}
// Since there can be more than one valid AssemblyQualifiedName for a given Type this
// check can return true for some cases which can cause a minor side effect of a new
// Switch being created instead of just being refreshed.
return sourceElement.SwitchType != currentType.AssemblyQualifiedName;
}
}
void CreateSwitch(string typeName, string name)
{
if (!string.IsNullOrEmpty(typeName))
{
traceSource.Switch = (SourceSwitch)TraceUtils.GetRuntimeObject(typeName, typeof(SourceSwitch), name);
}
else
{
traceSource.Switch = new SourceSwitch(name, traceSource.DefaultLevel.ToString());
}
}
}
private static void ConfigureTraceSettings()
{
// Ported from https://referencesource.microsoft.com/#System/compmod/system/diagnostics/TraceInternal.cs,06360b4de5e221c2, https://referencesource.microsoft.com/#System/compmod/system/diagnostics/TraceInternal.cs,37
TraceSection traceSection = DiagnosticsConfiguration.SystemDiagnosticsSection?.Trace;
if (traceSection != null)
{
Trace.UseGlobalLock = traceSection.UseGlobalLock;
Trace.AutoFlush = traceSection.AutoFlush;
Trace.IndentSize = traceSection.IndentSize;
ListenerElementsCollection listeners = DiagnosticsConfiguration.SystemDiagnosticsSection?.Trace.Listeners;
if (listeners != null)
{
// If listeners were configured, replace the defaults with these.
Trace.Listeners.Clear();
foreach (var listener in listeners.GetRuntimeObject())
{
Trace.Listeners.Add(listener);
}
}
}
}
private static void InitializingSwitch(object sender, InitializingSwitchEventArgs e)
{
Switch sw = e.Switch;
// Ported from https://referencesource.microsoft.com/#System/compmod/system/diagnostics/Switch.cs,173
SwitchElementsCollection switchSettings = DiagnosticsConfiguration.SwitchSettings;
if (switchSettings != null)
{
SwitchElement mySettings = switchSettings[sw.DisplayName];
if (mySettings != null)
{
if (mySettings.Value != null)
{
sw.Value = mySettings.Value;
}
else
{
sw.Value = sw.DefaultValue;
}
TraceUtils.CopyStringDictionary(sw.Attributes, mySettings.Attributes);
}
}
}
}
}