如何导出 Windows 系统和应用程序事件日志?

How to export Windows System and Application event log?

使用 EvtExportLog function,我目前无法为 Path and/or Query 参数指定正确的值。

我的目标是导出本地 ApplicationSystem 事件日志。

我试过:

EvtExportLog(
    IntPtr.Zero, 
    "Application", 
    "*", 
    "C:\SomePath\Application.evtx", 
    EventExportLogFlags.LogFilePath);

具有以下 P/Invoke 定义:

[Flags]
private enum EventExportLogFlags
{
    ChannelPath = 1,
    LogFilePath = 2,
    TolerateQueryErrors = 0x1000
};

[DllImport(@"wevtapi.dll", 
    CallingConvention = CallingConvention.Winapi,
    CharSet = CharSet.Auto,
    SetLastError = true)]
private static extern bool EvtExportLog(
    IntPtr sessionHandle,
    string path,
    string query,
    string targetPath,
    [MarshalAs(UnmanagedType.I4)] EventExportLogFlags flags);

不幸的是函数 returns false 和最后一个错误代码 2 (ERROR_FILE_NOT_FOUND).

我的问题:

PathQuery 参数中输入什么来导出本地应用程序和系统事件日志?

回答我自己的问题:

我的 PathQuery 实际上是正确的。有什么问题,是 Flags 参数。

我必须指定 EventExportLogFlags.ChannelPath 参数,而不是指定 EventExportLogFlags.LogFilePath 参数。

则导出成功:

EvtExportLog(
    IntPtr.Zero, 
    "Application", 
    "*", 
    "C:\SomePath\Application.evtx", 
    EventExportLogFlags.ChannelPath); // <-- HERE!