And/Or 对 select 一些事件日志记录的 XPath 查询

And/Or XPath query to select some Event Log records

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

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"表达式是为了让用户指定是否包含Informational messages或限制为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() + "]]" +
")" +
");"

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

我在您发布的代码中发现了两个问题。

  • 单引号没有在第 3 个名字上闭合:@Name='<3rd name>]] 应该是 @Name='<3rd name>']]
  • */System/Level 的第二个过滤器应该是 *[System[Level<=" + maxLevel.ToString() + "]]] "

从你的伪代码和你分享的内容来看,你似乎可以在 */System 的谓词过滤器中整合和移动你的一些逻辑,并使用 XPath,例如:

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

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

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

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

我需要明确查找(Level=1 或 Level=2)- Level <= 2 将获取各种 Microsoft "Information" 日志条目。

任何感兴趣的人 - 最后的工作查询是:

*[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)))]]