/
EventLogTraceListener.cs
177 lines (144 loc) · 5.38 KB
/
EventLogTraceListener.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
namespace System.Diagnostics
{
public sealed class EventLogTraceListener : TraceListener
{
private bool _nameSet;
public EventLogTraceListener()
{
}
public EventLogTraceListener(EventLog eventLog)
: base(eventLog != null ? eventLog.Source : string.Empty)
{
EventLog = eventLog;
}
public EventLogTraceListener(string source)
{
EventLog = new EventLog
{
Source = source
};
}
public EventLog EventLog
{
get;
set;
}
public override string Name
{
get
{
if (_nameSet == false && EventLog != null)
{
_nameSet = true;
base.Name = EventLog.Source;
}
return base.Name;
}
set
{
_nameSet = true;
base.Name = value;
}
}
public override void Close() => EventLog?.Close();
protected override void Dispose(bool disposing)
{
try
{
Close();
if (!disposing)
{
EventLog = null;
}
}
finally
{
base.Dispose(disposing);
}
}
public override void Write(string message) => EventLog?.WriteEntry(message);
public override void WriteLine(string message) => Write(message);
[ComVisible(false)]
public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType severity, int id, string format, params object[] args)
{
if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, format, args, null, null))
return;
EventInstance data = CreateEventInstance(severity, id);
if (args == null || args.Length == 0)
{
EventLog.WriteEvent(data, format);
}
else if (string.IsNullOrEmpty(format))
{
string[] strings = new string[args.Length];
for (int i = 0; i < args.Length; i++)
{
strings[i] = args[i].ToString();
}
EventLog.WriteEvent(data, strings);
}
else
{
EventLog.WriteEvent(data, string.Format(CultureInfo.InvariantCulture, format, args));
}
}
[ComVisible(false)]
public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType severity, int id, string message)
{
if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, message, null, null, null))
return;
EventLog.WriteEvent(CreateEventInstance(severity, id), message);
}
[ComVisible(false)]
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType severity, int id, object data)
{
if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, null, null, data, null))
return;
EventLog.WriteEvent(CreateEventInstance(severity, id), new object[] { data });
}
[ComVisible(false)]
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType severity, int id, params object[] data)
{
if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, null, null, null, data))
return;
EventInstance inst = CreateEventInstance(severity, id);
var sb = new StringBuilder();
if (data != null)
{
for (int i = 0; i < data.Length; i++)
{
if (i != 0)
sb.Append(", ");
if (data[i] != null)
sb.Append(data[i].ToString());
}
}
EventLog.WriteEvent(inst, new object[] { sb.ToString() });
}
private static EventInstance CreateEventInstance(TraceEventType severity, int id)
{
// Win32 EventLog has an implicit cap at ushort.MaxValue
// We need to cap this explicitly to prevent larger value
// being wrongly casted
if (id > ushort.MaxValue)
id = ushort.MaxValue;
// Ideally we need to pick a value other than '0' as zero is
// a commonly used EventId by most applications
if (id < ushort.MinValue)
id = ushort.MinValue;
EventInstance data = new EventInstance(id, 0);
if (severity == TraceEventType.Error || severity == TraceEventType.Critical)
data.EntryType = EventLogEntryType.Error;
else if (severity == TraceEventType.Warning)
data.EntryType = EventLogEntryType.Warning;
else
data.EntryType = EventLogEntryType.Information;
return data;
}
}
}