将事件查看器日志文件导出为A * .evtx文件

时间:2018-06-13 12:56:14

标签: c# event-viewer

我正在尝试从事件查看器中导出信息。

我尝试使用EventLogSession执行此操作,因此我可以使用.evtx格式而不仅仅是文本文件。

public static void ExportEventViewerLog(int YearsAgo = 0, int MonthsAgo = 0, int DaysAgo = 0)
{
        int Year = 0;
        int Month = 0;
        int Day = 0;

        if (YearsAgo != 0)
        {
            Year = YearsAgo;
        }
        else if (MonthsAgo != 0)
        {
            Month = MonthsAgo;
        }
        else if (DaysAgo != 0)
        {
            Day = DaysAgo;
        }

        DateTime previousDate = DateTime.Now.AddYears(-Year).AddMonths(-Month).AddDays(-Day);
        DateTime now = DateTime.Now.Date;

        Console.WriteLine(previousDate.ToString("yyyy-MM-dd"));
        //2018-06-12
        Console.WriteLine(now.ToString("hh:mm:ss"));
        //12:00:00

        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.evtx");
        string query = "<QueryList> " + $@"<Select Path=""Application"">*[System[(Level=1  or Level=2 or Level=3 or Level=4 or Level=0 or Level=5) and TimeCreated[@SystemTime&gt;='{previousDate.ToString("yyyy-MM-dd")}T{previousDate.ToString("hh:mm:ss")}.000Z' and @SystemTime&lt;='{now.ToString("yyyy-MM-dd")}T{now.ToString("hh:mm:ss")}.999Z']]]</Select> " + "</Query> " + "</QueryList>";

        EventLogSession eventLogSession = new EventLogSession();
        eventLogSession.ExportLogAndMessages("Application", PathType.LogName, query, path, false, CultureInfo.CurrentCulture);
}

以下是我如何称呼它

static void Main()
{
        ExportEventViewerLog(YearsAgo: 0, MonthsAgo: 0, DaysAgo: 1);
        Console.WriteLine("Press Any Key To Exit");
        Console.ReadKey();
}

这是错误

  

System.Diagnostics.Eventing.Reader.EventLogException

System.Diagnostics.Eventing.Reader.EventLogException HResult=0x80131500 Source=System.Core StackTrace: at System.Diagnostics.Eventing.Reader.EventLogException.Throw(Int32 errorCode) at System.Diagnostics.Eventing.Reader.NativeWrapper.EvtExportLog(EventLogHandle session, String channelPath, String query, String targetFilePath, Int32 flags) at System.Diagnostics.Eventing.Reader.EventLogSession.ExportLog(String path, PathType pathType, String query, String targetFilePath, Boolean tolerateQueryErrors) at System.Diagnostics.Eventing.Reader.EventLogSession.ExportLogAndMessages(String path, PathType pathType, String query, String targetFilePath, Boolean tolerateQueryErrors, CultureInfo targetCultureInfo) at app.Program.ExportEventViewerLog(Int32 YearsAgo, Int32 MonthsAgo, Int32 DaysAgo) in C:\Users\User\app\Program.cs:line 296 atapp_1._0.Program.Main() in C:\Users\User\Google Drive\app\Program.cs:line 34

注意:我不相信路径是问题,因为如果我将查询更改为通配符*,该方法将执行而没有任何错误,我从event viewer -> filter current log -> xml
以下是从事件查看器

生成的原始查询
<QueryList>
<Query Id="0" Path="Application">
<Select Path="Application">*[System[(Level=1  or Level=2 or Level=3 or Level=4 or Level=0 or Level=5) and TimeCreated[@SystemTime&gt;='1991-07-24T21:12:12.000Z' and @SystemTime&lt;='2018-06-12T21:12:12.999Z']]]</Select>

1 个答案:

答案 0 :(得分:1)

您缺少XML标记:

<Query Id=\"0\" Path=\"Application\">

修复此问题仍然导致我的异常,即使是管理员,但使用不同的路径修复它(可能是因为事件查看器服务缺少用户路径的权限)

string path = Path.Combine(Path.GetTempPath(), "test.evtx");