-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLogging.cs
More file actions
201 lines (182 loc) · 6.44 KB
/
Copy pathLogging.cs
File metadata and controls
201 lines (182 loc) · 6.44 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
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
201
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Security;
using System.Threading;
// ReSharper disable InconsistentNaming
namespace TSLab.Script.Handlers
{
// ReSharper disable once ConvertToStaticClass
internal sealed class Logging
{
private static volatile bool s_LoggingEnabled = true;
private static volatile bool s_LoggingInitialized;
private static volatile bool s_AppDomainShutdown;
private const string TraceSourceHandlersName = "TSLab.Script.Handlers.Options";
private static TraceSource s_DataCommonTraceSource;
private Logging()
{
}
private static object s_InternalSyncObject;
private static object InternalSyncObject
{
get
{
if (s_InternalSyncObject == null)
{
object o = new Object();
Interlocked.CompareExchange(ref s_InternalSyncObject, o, null);
}
return s_InternalSyncObject;
}
}
internal static bool On
{
get
{
if (!s_LoggingInitialized)
{
InitializeLogging();
}
return s_LoggingEnabled;
}
}
internal static TraceSource Handlers
{
get
{
if (!s_LoggingInitialized)
{
InitializeLogging();
}
if (!s_LoggingEnabled)
{
return null;
}
return s_DataCommonTraceSource;
}
}
private static void InitializeLogging()
{
lock (InternalSyncObject)
{
if (!s_LoggingInitialized)
{
bool loggingEnabled;
s_DataCommonTraceSource = new TraceSource(TraceSourceHandlersName);
try
{
loggingEnabled = s_DataCommonTraceSource.Switch.ShouldTrace(TraceEventType.Critical);
}
catch (SecurityException)
{
// These may throw if the caller does not have permission to hook up trace listeners.
// We treat this case as though logging were disabled.
Close();
loggingEnabled = false;
}
if (loggingEnabled)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += UnhandledExceptionHandler;
currentDomain.DomainUnload += AppDomainUnloadEvent;
currentDomain.ProcessExit += ProcessExitEvent;
}
s_LoggingEnabled = loggingEnabled;
s_LoggingInitialized = true;
}
}
}
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Logging functions must work in partial trust mode")]
private static void Close()
{
s_DataCommonTraceSource?.Close();
}
/// <devdoc>
/// <para>Logs any unhandled exception through this event handler</para>
/// </devdoc>
private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;
Exception(Handlers, "UnhandledExceptionHandler", e);
}
private static void ProcessExitEvent(object sender, EventArgs e)
{
Close();
s_AppDomainShutdown = true;
}
/// <devdoc>
/// <para>Called when the system is shutting down, used to prevent additional logging post-shutdown</para>
/// </devdoc>
private static void AppDomainUnloadEvent(object sender, EventArgs e)
{
Close();
s_AppDomainShutdown = true;
}
/// <devdoc>
/// <para>Confirms logging is enabled, given current logging settings</para>
/// </devdoc>
private static bool ValidateSettings(TraceSource traceSource, TraceEventType traceLevel)
{
if (!s_LoggingEnabled)
{
return false;
}
if (!s_LoggingInitialized)
{
InitializeLogging();
}
if (traceSource == null || !traceSource.Switch.ShouldTrace(traceLevel))
{
return false;
}
if (s_AppDomainShutdown)
{
return false;
}
return true;
}
internal static void PrintLine(TraceSource traceSource, TraceEventType eventType, int id, string msg)
{
traceSource.TraceEvent(eventType, id, msg);
}
internal static void Exception(TraceSource traceSource, string method, Exception e)
{
if (!ValidateSettings(traceSource, TraceEventType.Error))
{
return;
}
string infoLine = $"Exception in {method}:{e.Message}";
if (!string.IsNullOrWhiteSpace(e.StackTrace))
{
infoLine += "\r\n" + e.StackTrace;
}
PrintLine(traceSource, TraceEventType.Error, 0, infoLine);
}
internal static void PrintInfo(TraceSource traceSource, string objectName, string msg)
{
if (!ValidateSettings(traceSource, TraceEventType.Warning))
{
return;
}
var msg1 = $"[{objectName}] {msg}";
PrintLine(traceSource, TraceEventType.Warning, 0, msg1);
}
internal static void PrintWarning(TraceSource traceSource, string msg)
{
if (!ValidateSettings(traceSource, TraceEventType.Warning))
{
return;
}
PrintLine(traceSource, TraceEventType.Warning, 0, msg);
}
internal static void PrintError(TraceSource traceSource, string msg)
{
if (!ValidateSettings(traceSource, TraceEventType.Error))
{
return;
}
PrintLine(traceSource, TraceEventType.Error, 0, msg);
}
}
}