【问题标题】:How can I query the event log for a specific source using xpath?如何使用 xpath 查询特定源的事件日志?
【发布时间】:2025-12-24 15:10:07
【问题描述】:

我想在整个 Windows 事件日志(例如应用程序)中查询由特定源(例如 MSSQL$SQLEXPRESS)写入的事件。我已经编写了工作代码来搜索事件 ID:

string xpathQuery = string.Format("*[System/EventID={0}]", intFilter);
EventLogQuery query = new EventLogQuery(eventLogName, PathType.LogName, xpathQuery);
EventLogReader reader = new EventLogReader(query);
for (EventRecord eventInstance = reader.ReadEvent(); null != eventInstance; eventInstance = reader.ReadEvent())
{
    lisRecords.Add(eventInstance);
}

我必须如何更改 xpathQuery,才能搜索 4 个 eventlog-entry-sources?

【问题讨论】:

    标签: c# windows xpath event-log


    【解决方案1】:

    像这样更改查询字符串(您可能希望创建一个文本资源并将此查询放入其中以避免转义):

    *[System[Provider[@Name='Microsoft-Windows-ADSI' or @Name='Outlook'] and (EventID=1 or EventID=2 or EventID=3)]]
    

    以上等价于:

    (EventID in (1,2,3)) and (Source in ('Microsoft-Windows-ADSI', 'Outlook'))
    

    【讨论】:

    • 但是如何为过滤器正确转义创建的时间? *[[System/Provider/@Name=\"MySource\"] and TimeCreated[timediff(@SystemTime) & lt;= 2592000000]]]