Repository navigation
Expand file tree
/
Copy pathWindowsEventReader.cs
More file actions
640 lines (570 loc) · 26.8 KB
/
Copy pathWindowsEventReader.cs
File metadata and controls
640 lines (570 loc) · 26.8 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Xml.Linq;
using Microsoft.Win32.SafeHandles;
namespace EventFast;
internal sealed record EventRow(
DateTime Time,
string Level,
int EventId,
string Provider,
string Channel,
long RecordId,
string Computer,
string Details,
string Xml,
string? LogFilePath = null,
string? Message = null)
{
public string DisplayLevel => Localization.Level(Level);
}
internal static class WindowsEventReader
{
private const int ErrorInsufficientBuffer = 122;
private const int ErrorNoMoreItems = 259;
private const int ErrorTimeout = 1460;
private const int EvtQueryChannelPath = 0x1;
private const int EvtQueryFilePath = 0x2;
private const int EvtQueryReverseDirection = 0x200;
private const int EvtRenderEventValues = 0;
private const int EvtRenderEventXml = 1;
private const int EvtRenderContextSystem = 1;
private const int EvtRenderContextUser = 2;
private const int EvtFormatMessageEvent = 1;
private const int BatchSize = 256;
internal static IReadOnlyList<EventRow> Read(string channel, string xpath, CancellationToken cancellationToken,
int maximumRows = 1_000_000, Action<IReadOnlyList<EventRow>>? firstBatch = null, bool filePath = false,
bool failIfTruncated = false, bool includeMessage = false, bool includeXml = false)
{
using var query = EvtQuery(IntPtr.Zero, channel, xpath, (filePath ? EvtQueryFilePath : EvtQueryChannelPath) | EvtQueryReverseDirection);
if (query.IsInvalid)
throw NativeError(channel);
using var renderer = new SystemRenderer();
using var userRenderer = new UserRenderer();
using var messageFormatter = includeMessage ? new MessageFormatter() : null;
var rows = new List<EventRow>();
var handles = new IntPtr[BatchSize];
var discardedRows = false;
// ponytail: one million matches the documented UI ceiling; add disk-backed paging only beyond it.
while (rows.Count < maximumRows)
{
cancellationToken.ThrowIfCancellationRequested();
if (!EvtNext(query, handles.Length, handles, 0, 0, out var returned))
{
var error = Marshal.GetLastWin32Error();
if (error is ErrorNoMoreItems or ErrorTimeout)
break;
throw NativeError(channel, error);
}
var count = Math.Min(returned, maximumRows - rows.Count);
discardedRows |= returned > count;
var ownedHandles = new EventHandle[count];
try
{
var batch = new EventRow[count];
for (var index = 0; index < count; index++)
{
cancellationToken.ThrowIfCancellationRequested();
var handle = new EventHandle(handles[index]);
ownedHandles[index] = handle;
handles[index] = IntPtr.Zero;
var row = renderer.Render(handle, filePath ? channel : null);
var details = userRenderer.TryRender(handle);
var xml = details is null || includeXml ? RenderXml(handle) : "";
row = details is null ? AddDetails(row, xml) : row with { Details = details };
if (includeXml)
row = row with { Xml = xml };
batch[index] = messageFormatter is null ? row : AddMessage(row, messageFormatter.Format(handle, row));
}
if (firstBatch is not null)
{
firstBatch(batch);
firstBatch = null;
}
rows.AddRange(batch);
}
finally
{
foreach (var handle in ownedHandles)
handle?.Dispose();
for (var index = 0; index < returned; index++)
{
if (handles[index] != IntPtr.Zero)
{
EvtClose(handles[index]);
handles[index] = IntPtr.Zero;
}
}
}
}
if (failIfTruncated && rows.Count == maximumRows)
{
cancellationToken.ThrowIfCancellationRequested();
if (discardedRows)
throw ResultLimit(maximumRows);
if (EvtNext(query, handles.Length, handles, 0, 0, out var returned))
{
for (var index = 0; index < returned; index++)
{
EvtClose(handles[index]);
handles[index] = IntPtr.Zero;
}
throw ResultLimit(maximumRows);
}
var error = Marshal.GetLastWin32Error();
if (error is not ErrorNoMoreItems and not ErrorTimeout)
throw NativeError(channel, error);
}
return rows;
}
private static InvalidOperationException ResultLimit(int maximumRows) =>
new(Localization.Format("ResultLimit", maximumRows));
internal static string ReadMessage(EventRow row)
{
using var formatter = new MessageFormatter();
return formatter.Format(row);
}
internal static MessageFormatter CreateMessageFormatter() => new();
internal static string ReadXml(EventRow row)
{
if (row.Xml.Length > 0)
return row.Xml;
var xpath = $"*[System[EventRecordID={row.RecordId}]]";
using var query = EvtQuery(IntPtr.Zero, row.LogFilePath ?? row.Channel, xpath,
row.LogFilePath is null ? EvtQueryChannelPath : EvtQueryFilePath);
if (query.IsInvalid)
return row.Xml;
var events = new IntPtr[1];
if (!EvtNext(query, 1, events, 0, 0, out var returned) || returned == 0)
return row.Xml;
using var handle = new EventHandle(events[0]);
return RenderXml(handle);
}
internal sealed class MessageFormatter : IDisposable
{
private const int PreloadChunkSize = 1000;
// A gap this small costs less to read through than one extra EvtQuery, which scans the log.
private const long PreloadMaximumGap = 64;
private readonly Dictionary<(string Provider, string? File), EventHandle?> _metadata = [];
private Dictionary<(string Source, long RecordId), (string Message, string Xml)> _preloaded = [];
internal string Format(EventRow row) => ReadContent(row, includeXml: false).Message;
// Streams rows in chunks and loads each chunk's content with record-ID range queries instead of one query per row;
// ReadContent then answers from the current chunk, so callers must read content while enumerating.
internal IEnumerable<EventRow> Preload(IEnumerable<EventRow> rows, bool includeXml, CancellationToken cancellationToken) =>
rows.Chunk(PreloadChunkSize).SelectMany(chunk =>
{
_preloaded = ReadRanges(chunk, includeXml, cancellationToken);
return chunk;
});
private static Dictionary<(string Source, long RecordId), (string Message, string Xml)> ReadRanges(
IEnumerable<EventRow> rows, bool includeXml, CancellationToken cancellationToken)
{
var contents = new Dictionary<(string Source, long RecordId), (string Message, string Xml)>();
foreach (var source in rows.Where(row => row.Message is null || includeXml && row.Xml.Length == 0)
.GroupBy(row => (Path: row.LogFilePath ?? row.Channel, IsFile: row.LogFilePath is not null)))
{
var ids = source.Select(row => row.RecordId).Distinct().Order().ToArray();
for (var start = 0; start < ids.Length;)
{
var end = start;
while (end + 1 < ids.Length && ids[end + 1] - ids[end] <= PreloadMaximumGap)
end++;
try
{
foreach (var row in Read(source.Key.Path, $"*[System[EventRecordID >= {ids[start]} and EventRecordID <= {ids[end]}]]",
cancellationToken, filePath: source.Key.IsFile, includeMessage: true, includeXml: includeXml))
contents[(source.Key.Path, row.RecordId)] = (row.Message ?? row.Details, row.Xml);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
// ReadContent falls back to the single-event path for rows this range could not load.
}
start = end + 1;
}
}
return contents;
}
internal (string Message, string Xml) ReadContent(EventRow row, bool includeXml)
{
if (row.Message is not null && (!includeXml || row.Xml.Length > 0))
return (row.Message, row.Xml);
if (_preloaded.TryGetValue((row.LogFilePath ?? row.Channel, row.RecordId), out var preloaded))
return preloaded;
var xpath = $"*[System[EventRecordID={row.RecordId}]]";
using var query = EvtQuery(IntPtr.Zero, row.LogFilePath ?? row.Channel, xpath, row.LogFilePath is null ? EvtQueryChannelPath : EvtQueryFilePath);
if (query.IsInvalid)
return (row.Message ?? row.Details, row.Xml);
var events = new IntPtr[1];
if (!EvtNext(query, 1, events, 0, 0, out var returned) || returned == 0)
return (row.Message ?? row.Details, row.Xml);
using var handle = new EventHandle(events[0]);
return (row.Message ?? Format(handle, row), includeXml && row.Xml.Length == 0 ? RenderXml(handle) : row.Xml);
}
internal string Format(EventHandle handle, EventRow row)
{
var key = (row.Provider, row.LogFilePath);
if (!_metadata.TryGetValue(key, out var metadata))
{
metadata = EvtOpenPublisherMetadata(IntPtr.Zero, row.Provider, row.LogFilePath, 0, 0);
if (metadata.IsInvalid)
{
metadata.Dispose();
metadata = null;
}
// A missing publisher is remembered too, so its other events do not reopen the metadata.
_metadata.Add(key, metadata);
}
if (metadata is null)
return row.Details;
EvtFormatMessage(metadata, handle, 0, 0, IntPtr.Zero, EvtFormatMessageEvent, 0, IntPtr.Zero, out var size);
if (Marshal.GetLastWin32Error() != ErrorInsufficientBuffer || size == 0)
return row.Details;
var buffer = Marshal.AllocHGlobal(checked(size * sizeof(char)));
try
{
return EvtFormatMessage(metadata, handle, 0, 0, IntPtr.Zero, EvtFormatMessageEvent, size, buffer, out _)
? Marshal.PtrToStringUni(buffer) ?? row.Details
: row.Details;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
public void Dispose()
{
foreach (var handle in _metadata.Values)
handle?.Dispose();
_metadata.Clear();
}
}
private static string RenderXml(EventHandle handle)
{
EvtRender(IntPtr.Zero, handle, EvtRenderEventXml, 0, IntPtr.Zero, out var size, out _);
var error = Marshal.GetLastWin32Error();
if (error != ErrorInsufficientBuffer)
throw NativeError("event", error);
var buffer = Marshal.AllocHGlobal(size);
try
{
if (!EvtRender(IntPtr.Zero, handle, EvtRenderEventXml, size, buffer, out _, out _))
throw NativeError("event");
return Marshal.PtrToStringUni(buffer) ?? "";
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
private static EventRow AddDetails(EventRow row, string xml)
{
var root = XDocument.Parse(xml).Root ?? throw new InvalidDataException(Localization.Text("EmptyEventXml"));
var details = string.Join(Environment.NewLine,
root.Descendants().Where(element => !element.HasElements &&
element.Ancestors().Any(ancestor => ancestor.Name.LocalName is "EventData" or "UserData"))
.Select(element => element.Value).Where(value => !string.IsNullOrWhiteSpace(value)));
return row with { Details = details };
}
private static EventRow AddMessage(EventRow row, string message) =>
string.IsNullOrWhiteSpace(message)
? row
: row with
{
Message = message,
Details = row.Details.Contains(message, StringComparison.Ordinal) ? row.Details :
string.IsNullOrWhiteSpace(row.Details) ? message : $"{row.Details}{Environment.NewLine}{message}"
};
private sealed class SystemRenderer : IDisposable
{
private const int PropertyCount = 18;
private const int TypeMask = 0x7f;
private const int TypeArray = 0x80;
private const int TypeNull = 0;
private const int TypeString = 1;
private const int TypeByte = 4;
private const int TypeUInt16 = 6;
private const int TypeUInt64 = 10;
private const int TypeFileTime = 17;
private readonly EventHandle _context;
private IntPtr _buffer;
private int _capacity;
internal SystemRenderer()
{
if (IntPtr.Size != 8 || Marshal.SizeOf<EvtVariant>() != 16)
throw new PlatformNotSupportedException(Localization.Text("WindowsX64Only"));
_context = EvtCreateRenderContext(0, IntPtr.Zero, EvtRenderContextSystem);
if (_context.IsInvalid)
throw NativeError("system render context");
}
internal EventRow Render(EventHandle handle, string? logFilePath)
{
if (!EvtRender(_context, handle, EvtRenderEventValues, _capacity, _buffer, out var used, out var count))
{
var error = Marshal.GetLastWin32Error();
if (error != ErrorInsufficientBuffer || used <= 0)
throw NativeError("event system fields", error);
Resize(used);
if (!EvtRender(_context, handle, EvtRenderEventValues, _capacity, _buffer, out used, out count))
throw NativeError("event system fields");
}
if (count < PropertyCount || used < checked(count * 16))
throw new InvalidDataException(Localization.Format("InvalidSystemBuffer", count, used));
var time = Value(8, TypeFileTime);
var level = Number(4, TypeByte);
return new EventRow(
time.Type == TypeNull ? DateTime.MinValue : DateTime.FromFileTimeUtc(checked((long)time.UInt64)).ToLocalTime(),
level switch { 1 => "Critical", 2 => "Error", 3 => "Warning", 4 => "Information", 5 => "Verbose", _ => "Unknown" },
checked((int)Number(2, TypeUInt16)),
Text(0),
Text(14),
checked((long)Number(9, TypeUInt64)),
Text(15),
"",
"",
logFilePath);
}
private EvtVariant Value(int index, int expectedType)
{
var value = Marshal.PtrToStructure<EvtVariant>(IntPtr.Add(_buffer, index * 16));
var type = value.Type & TypeMask;
if ((value.Type & TypeArray) != 0 || type is not TypeNull && type != expectedType)
throw new InvalidDataException(Localization.Format("InvalidSystemType", index, expectedType, type));
value.Type = type;
return value;
}
private ulong Number(int index, int expectedType)
{
var value = Value(index, expectedType);
return value.Type == TypeNull ? 0 : expectedType switch
{
TypeByte => value.Byte,
TypeUInt16 => value.UInt16,
TypeUInt64 => value.UInt64,
_ => throw new InvalidOperationException(Localization.Format("UnsupportedValueType", expectedType))
};
}
private string Text(int index)
{
var value = Value(index, TypeString);
return value.Type == TypeNull ? "" : Marshal.PtrToStringUni(value.Pointer) ?? "";
}
private void Resize(int size)
{
_buffer = _buffer == IntPtr.Zero
? Marshal.AllocHGlobal(size)
: Marshal.ReAllocHGlobal(_buffer, size);
_capacity = size;
}
public void Dispose()
{
_context.Dispose();
if (_buffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(_buffer);
_buffer = IntPtr.Zero;
}
}
}
private sealed class UserRenderer : IDisposable
{
private const int TypeMask = 0x7f;
private const int TypeArray = 0x80;
private const int TypeNull = 0;
private const int TypeString = 1;
private const int TypeAnsiString = 2;
private const int TypeSByte = 3;
private const int TypeByte = 4;
private const int TypeInt16 = 5;
private const int TypeUInt16 = 6;
private const int TypeInt32 = 7;
private const int TypeUInt32 = 8;
private const int TypeInt64 = 9;
private const int TypeUInt64 = 10;
private const int TypeSingle = 11;
private const int TypeDouble = 12;
private const int TypeBoolean = 13;
private const int TypeBinary = 14;
private const int TypeGuid = 15;
private const int TypeSizeT = 16;
private const int TypeFileTime = 17;
private const int TypeSystemTime = 18;
private const int TypeSid = 19;
private const int TypeHexInt32 = 20;
private const int TypeHexInt64 = 21;
private const int TypeXml = 35;
private readonly EventHandle _context;
private IntPtr _buffer;
private int _capacity;
internal UserRenderer()
{
_context = EvtCreateRenderContext(0, IntPtr.Zero, EvtRenderContextUser);
if (_context.IsInvalid)
throw NativeError("user render context");
}
internal string? TryRender(EventHandle handle)
{
if (!EvtRender(_context, handle, EvtRenderEventValues, _capacity, _buffer, out var used, out var count))
{
var error = Marshal.GetLastWin32Error();
if (error != ErrorInsufficientBuffer || used <= 0)
throw NativeError("event data", error);
Resize(used);
if (!EvtRender(_context, handle, EvtRenderEventValues, _capacity, _buffer, out used, out count))
throw NativeError("event data");
}
if (count == 0)
return "";
if (used < checked(count * 16))
throw new InvalidDataException(Localization.Format("InvalidEventDataBuffer", count, used));
var values = new List<string>(count);
for (var index = 0; index < count; index++)
{
var value = Marshal.PtrToStructure<EvtVariant>(IntPtr.Add(_buffer, index * 16));
if ((value.Type & TypeArray) != 0)
// ponytail: rare array variants keep the exact XML fallback; add array conversion only if profiling shows it matters.
return null;
var text = Text(value, value.Type & TypeMask);
if (text is null)
return null;
if (!string.IsNullOrWhiteSpace(text))
values.Add(text.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n'));
}
return string.Join(Environment.NewLine, values);
}
private static string? Text(EvtVariant value, int type)
{
try
{
return type switch
{
TypeNull => "",
TypeString or TypeXml => Marshal.PtrToStringUni(value.Pointer) ?? "",
TypeAnsiString => Marshal.PtrToStringAnsi(value.Pointer) ?? "",
TypeSByte => value.SByte.ToString(CultureInfo.InvariantCulture),
TypeByte => value.Byte.ToString(CultureInfo.InvariantCulture),
TypeInt16 => value.Int16.ToString(CultureInfo.InvariantCulture),
TypeUInt16 => value.UInt16.ToString(CultureInfo.InvariantCulture),
TypeInt32 => value.Int32.ToString(CultureInfo.InvariantCulture),
TypeUInt32 => value.UInt32.ToString(CultureInfo.InvariantCulture),
TypeInt64 => value.Int64.ToString(CultureInfo.InvariantCulture),
TypeUInt64 or TypeSizeT => value.UInt64.ToString(CultureInfo.InvariantCulture),
TypeSingle => value.Single.ToString(CultureInfo.InvariantCulture),
TypeDouble => value.Double.ToString(CultureInfo.InvariantCulture),
TypeBoolean => value.Int32 == 0 ? "false" : "true",
TypeBinary => Binary(value),
TypeGuid => value.Pointer == IntPtr.Zero ? "" : Marshal.PtrToStructure<Guid>(value.Pointer).ToString("B"),
TypeFileTime => DateTime.FromFileTimeUtc(checked((long)value.UInt64)).ToString("O", CultureInfo.InvariantCulture),
TypeSystemTime => SystemTimeText(value.Pointer),
TypeSid => value.Pointer == IntPtr.Zero ? "" : new SecurityIdentifier(value.Pointer).Value,
TypeHexInt32 => $"0x{value.UInt32:x}",
TypeHexInt64 => $"0x{value.UInt64:x}",
_ => null
};
}
catch (Exception exception) when (exception is ArgumentException or OverflowException)
{
return null;
}
}
private static string Binary(EvtVariant value)
{
if (value.Pointer == IntPtr.Zero || value.Count == 0)
return "";
var bytes = new byte[value.Count];
Marshal.Copy(value.Pointer, bytes, 0, bytes.Length);
return Convert.ToHexString(bytes);
}
private static string SystemTimeText(IntPtr pointer)
{
if (pointer == IntPtr.Zero)
return "";
var value = Marshal.PtrToStructure<SystemTime>(pointer);
return new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, value.Milliseconds,
DateTimeKind.Utc).ToString("O", CultureInfo.InvariantCulture);
}
private void Resize(int size)
{
_buffer = _buffer == IntPtr.Zero ? Marshal.AllocHGlobal(size) : Marshal.ReAllocHGlobal(_buffer, size);
_capacity = size;
}
public void Dispose()
{
_context.Dispose();
if (_buffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(_buffer);
_buffer = IntPtr.Zero;
}
}
}
[StructLayout(LayoutKind.Explicit, Size = 16)]
private struct EvtVariant
{
[FieldOffset(0)] internal IntPtr Pointer;
[FieldOffset(0)] internal sbyte SByte;
[FieldOffset(0)] internal byte Byte;
[FieldOffset(0)] internal short Int16;
[FieldOffset(0)] internal ushort UInt16;
[FieldOffset(0)] internal int Int32;
[FieldOffset(0)] internal uint UInt32;
[FieldOffset(0)] internal long Int64;
[FieldOffset(0)] internal ulong UInt64;
[FieldOffset(0)] internal float Single;
[FieldOffset(0)] internal double Double;
[FieldOffset(8)] internal int Count;
[FieldOffset(12)] internal int Type;
}
[StructLayout(LayoutKind.Sequential)]
private struct SystemTime
{
internal ushort Year;
internal ushort Month;
internal ushort DayOfWeek;
internal ushort Day;
internal ushort Hour;
internal ushort Minute;
internal ushort Second;
internal ushort Milliseconds;
}
private static Exception NativeError(string target, int? code = null)
{
var error = code ?? Marshal.GetLastWin32Error();
return error switch
{
5 => new UnauthorizedAccessException(Localization.Format("AccessDenied", target)),
15007 => new InvalidOperationException(Localization.Format("MissingChannel", target)),
_ => new Win32Exception(error, Localization.Format("ReadFailed", target))
};
}
internal sealed class EventHandle : SafeHandleZeroOrMinusOneIsInvalid
{
internal EventHandle() : base(true) { }
internal EventHandle(IntPtr handle) : base(true) => SetHandle(handle);
protected override bool ReleaseHandle() => EvtClose(handle);
}
[DllImport("wevtapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern EventHandle EvtQuery(IntPtr session, string path, string query, int flags);
[DllImport("wevtapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern EventHandle EvtOpenPublisherMetadata(IntPtr session, string publisherId, string? logFilePath, int locale, int flags);
[DllImport("wevtapi.dll", SetLastError = true)]
private static extern EventHandle EvtCreateRenderContext(int valuePathsCount, IntPtr valuePaths, int flags);
[DllImport("wevtapi.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EvtNext(EventHandle resultSet, int eventArraySize, [Out] IntPtr[] events, int timeout, int flags, out int returned);
[DllImport("wevtapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EvtRender(IntPtr context, EventHandle fragment, int flags, int bufferSize, IntPtr buffer, out int bufferUsed, out int propertyCount);
[DllImport("wevtapi.dll", EntryPoint = "EvtRender", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EvtRender(EventHandle context, EventHandle fragment, int flags, int bufferSize, IntPtr buffer, out int bufferUsed, out int propertyCount);
[DllImport("wevtapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EvtFormatMessage(EventHandle publisherMetadata, EventHandle eventHandle, int messageId, int valueCount,
IntPtr values, int flags, int bufferSize, IntPtr buffer, out int bufferUsed);
[DllImport("wevtapi.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EvtClose(IntPtr handle);
}