首页 > 解决方案 > 和/或 XPath 查询以选择一些事件日志记录

问题描述

我用谷歌搜索了很多可能的答案,但没有运气。我正在尝试从事件日志(伪代码)中提取以下内容:

select events
where
    event date/time between FromDateTime and ToDateTime
and
   ((Level<=2)  //  error, critical only
    or 
    ((Level<=x) and Provider[Name] in a specific list)  // any messages for these apps
   )

(第二个“Level”表达式是允许用户指定是包含Informal消息还是限制为Warnings及以上,所以我不能直接丢弃它。)

以下是我尝试使用的(最新)表达式 - 未成功。

    string queryString = 
"*[System[TimeCreated[@SystemTime>='" + dFrom + "' and @SystemTime<='" + dTo + "']]] " +
" and " +
"(*[System[Level<=2]]" +
" or " +
" ( " + 
" *[System[Provider[@Name='<1st name>' or @Name='<2nd name>' or @Name='<3rd name>]] " + 
" and " +
"System[Level<=" + maxLevel.ToString() + "]]" +
")" +
");"

我是否试图创建一个对于事件日志查询评估器来说太难的表达式,或者我只是在表达式中有一个简单的错误?我一直在尝试各种形式的表达。似乎“级别”过滤器只是被忽略了,但为什么呢?

标签: c#xpathevent-logreadeventlog

解决方案


*** 啊!!- 我想我找到了。事件日志级别枚举为:

1 - Critical alert
2 - Error
3 - Warning
4 - Informational
5 - Logs at all levels
  ... and ...
0 - Undefined - indicates logs at all levels

事实证明,来自 Microsoft 组件的一些“信息”日志条目使用 0 级而不是 4 级,因此过滤器会拾取这些条目。我认为日志条目(尤其是 Microsoft 的)会使用适当级别的假设是错误的。

我需要明确查找(Level=1 或 Level=2)——Level <= 2 将获取各种 Microsoft“信息”日志条目。

对于任何有兴趣的人 - 最终的工作查询是:

*[System[TimeCreated[@SystemTime>='2018-07-30T17:22:30.000Z' 
    and @SystemTime<='2018-07-30T20:22:30.000Z']  
and (Level=1 or Level=2 or
  (Provider[@Name='Application Error' or @Name='Application Hang']
  and (Level=1 or Level=2 or Level=3 or Level=4)))]]

推荐阅读