/
EventLogSession.cs
258 lines (217 loc) · 8.88 KB
/
EventLogSession.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Security;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Win32;
namespace System.Diagnostics.Eventing.Reader
{
/// <summary>
/// Session Login Type
/// </summary>
public enum SessionAuthentication
{
Default = 0,
Negotiate = 1,
Kerberos = 2,
Ntlm = 3
}
/// <summary>
/// The type: log / external log file to query
/// </summary>
public enum PathType
{
LogName = 1,
FilePath = 2
}
/// <summary>
/// Defines a session for Event Log operations. The session can
/// be configured for a remote machine and can use specific
/// user credentials.
/// </summary>
public class EventLogSession : IDisposable
{
//
// the two context handles for rendering (for EventLogRecord).
// the system and user context handles. They are both common for all the event instances and can be created only once.
// access to the data member references is safe, while
// invoking methods on it is marked SecurityCritical as appropriate.
//
internal EventLogHandle renderContextHandleSystem = EventLogHandle.Zero;
internal EventLogHandle renderContextHandleUser = EventLogHandle.Zero;
// The dummy sync object for the two contexts.
private readonly object _syncObject;
private readonly string _server;
private readonly string _user;
private readonly string _domain;
private readonly SessionAuthentication _logOnType;
// Setup the System Context, once for all the EventRecords.
internal void SetupSystemContext()
{
if (!this.renderContextHandleSystem.IsInvalid)
return;
lock (_syncObject)
{
if (this.renderContextHandleSystem.IsInvalid)
{
// Create the SYSTEM render context
// Call the EvtCreateRenderContext to get the renderContextHandleSystem, so that we can get the system/values/user properties.
this.renderContextHandleSystem = NativeWrapper.EvtCreateRenderContext(0, null, UnsafeNativeMethods.EvtRenderContextFlags.EvtRenderContextSystem);
}
}
}
internal void SetupUserContext()
{
lock (_syncObject)
{
if (this.renderContextHandleUser.IsInvalid)
{
// Create the USER render context
this.renderContextHandleUser = NativeWrapper.EvtCreateRenderContext(0, null, UnsafeNativeMethods.EvtRenderContextFlags.EvtRenderContextUser);
}
}
}
public EventLogSession()
{
// handle = EventLogHandle.Zero;
_syncObject = new object();
}
public EventLogSession(string server)
:
this(server, null, null, (SecureString)null, SessionAuthentication.Default)
{
}
public EventLogSession(string server, string domain, string user, SecureString password, SessionAuthentication logOnType)
{
server ??= "localhost";
_syncObject = new object();
_server = server;
_domain = domain;
_user = user;
_logOnType = logOnType;
UnsafeNativeMethods.EvtRpcLogin erLogin = default;
erLogin.Server = _server;
erLogin.User = _user;
erLogin.Domain = _domain;
erLogin.Flags = (int)_logOnType;
erLogin.Password = CoTaskMemUnicodeSafeHandle.Zero;
try
{
if (password != null)
erLogin.Password.SetMemory(SecureStringMarshal.SecureStringToCoTaskMemUnicode(password));
// Open a session using the erLogin structure.
Handle = NativeWrapper.EvtOpenSession(UnsafeNativeMethods.EvtLoginClass.EvtRpcLogin, ref erLogin, 0, 0);
}
finally
{
erLogin.Password.Dispose();
}
}
internal EventLogHandle Handle { get; } = EventLogHandle.Zero;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this == s_globalSession)
throw new InvalidOperationException();
}
if (this.renderContextHandleSystem != null &&
!this.renderContextHandleSystem.IsInvalid)
this.renderContextHandleSystem.Dispose();
if (this.renderContextHandleUser != null &&
!this.renderContextHandleUser.IsInvalid)
this.renderContextHandleUser.Dispose();
if (Handle != null && !Handle.IsInvalid)
Handle.Dispose();
}
public void CancelCurrentOperations()
{
NativeWrapper.EvtCancel(Handle);
}
private static readonly EventLogSession s_globalSession = new EventLogSession();
public static EventLogSession GlobalSession
{
get { return s_globalSession; }
}
public IEnumerable<string> GetProviderNames()
{
List<string> namesList = new List<string>(100);
using (EventLogHandle ProviderEnum = NativeWrapper.EvtOpenProviderEnum(this.Handle, 0))
{
bool finish = false;
do
{
string s = NativeWrapper.EvtNextPublisherId(ProviderEnum, ref finish);
if (finish == false)
namesList.Add(s);
}
while (finish == false);
return namesList;
}
}
public IEnumerable<string> GetLogNames()
{
List<string> namesList = new List<string>(100);
using (EventLogHandle channelEnum = NativeWrapper.EvtOpenChannelEnum(this.Handle, 0))
{
bool finish = false;
do
{
string s = NativeWrapper.EvtNextChannelPath(channelEnum, ref finish);
if (finish == false)
namesList.Add(s);
}
while (finish == false);
return namesList;
}
}
public EventLogInformation GetLogInformation(string logName, PathType pathType)
{
ArgumentNullException.ThrowIfNull(logName);
return new EventLogInformation(this, logName, pathType);
}
public void ExportLog(string path, PathType pathType, string query, string targetFilePath)
{
this.ExportLog(path, pathType, query, targetFilePath, false);
}
public void ExportLog(string path, PathType pathType, string query, string targetFilePath, bool tolerateQueryErrors)
{
ArgumentNullException.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(targetFilePath);
UnsafeNativeMethods.EvtExportLogFlags flag = pathType switch
{
PathType.LogName => UnsafeNativeMethods.EvtExportLogFlags.EvtExportLogChannelPath,
PathType.FilePath => UnsafeNativeMethods.EvtExportLogFlags.EvtExportLogFilePath,
_ => throw new ArgumentOutOfRangeException(nameof(pathType)),
};
if (tolerateQueryErrors == false)
NativeWrapper.EvtExportLog(this.Handle, path, query, targetFilePath, (int)flag);
else
NativeWrapper.EvtExportLog(this.Handle, path, query, targetFilePath, (int)flag | (int)UnsafeNativeMethods.EvtExportLogFlags.EvtExportLogTolerateQueryErrors);
}
public void ExportLogAndMessages(string path, PathType pathType, string query, string targetFilePath)
{
this.ExportLogAndMessages(path, pathType, query, targetFilePath, false, CultureInfo.CurrentCulture);
}
public void ExportLogAndMessages(string path, PathType pathType, string query, string targetFilePath, bool tolerateQueryErrors, CultureInfo targetCultureInfo)
{
ExportLog(path, pathType, query, targetFilePath, tolerateQueryErrors);
// Ignore the CultureInfo, pass 0 to use the calling thread's locale
NativeWrapper.EvtArchiveExportedLog(this.Handle, targetFilePath, 0, 0);
}
public void ClearLog(string logName)
{
this.ClearLog(logName, null);
}
public void ClearLog(string logName, string backupPath)
{
ArgumentNullException.ThrowIfNull(logName);
NativeWrapper.EvtClearLog(this.Handle, logName, backupPath, 0);
}
}
}