用于解析%%变量的API

时间:2014-03-05 18:40:56

标签: c# windows event-log

我正在使用Windows中的C#事件日志API(基本上是System.Diagnostics.Eventing.Reader中的所有内容)。

我有一个EventMetadata对象并拉出其Description属性来检索事件的消息模板。 通常,这些模板看起来类似于以下内容:

Network interface reports message %1

每当我收到一个事件时,这些变量很容易被实际数据替换。 (EventLogRecord.Properties与占位符匹配)

这是我的问题所在。EventLogRecord.Properties有时包含不同类型的占位符。这些总是以%%开头,我找不到解决它们的方法。

举个例子:

// This method is triggered when a new event comes in
async public static void ListenerEvent(object s, EventRecordWrittenEventArgs args) {
  var evt = (EventLogRecord)args.EventRecord;

  // This method retrieves the template from a ProviderMetadata object
  // And replaces all %n with {n}
  // So that we can string.Format on it
  var tmp = TemplateCache.TemplateFor(evt);

  // Need this since the indices start with 1, not 0
  var props = new List<object> {string.Empty};
  props.AddRange(evt.Properties.Select(prop => prop.Value));

  // Now the message should be human-readable
  var msg = string.Format(tmp, props);
}

使用上面的示例模板,属性可能是[“%% 16411”],现在我最终得到以下消息

Network interface reports message %%16411

我现在的问题是,如何替换此%%16411? 我已查看ProviderMetadata及其余属性但似乎没有匹配。

任何有关如何解决这些占位符(甚至它们是什么/它们来自哪里)的帮助都表示赞赏。 显示此行为的事件是5152,如下所示:http://community.spiceworks.com/windows_event/show/452-microsoft-windows-security-auditing-5152

谢谢。

0 个答案:

没有答案