C# 解析%%变量的API
我正在Windows中使用C#事件日志API(基本上是Windows中的所有内容) 我有一个C# 解析%%变量的API,c#,windows,event-log,C#,Windows,Event Log,我正在Windows中使用C#事件日志API(基本上是Windows中的所有内容) 我有一个EventMetadata对象,并拉取它的Description属性来检索事件的消息模板。 通常,这些模板类似于以下内容: Network interface reports message %1 每当我收到一个事件时,这些变量很容易用实际数据替换。 (EventLogRecord.Properties匹配占位符) 这就是我的问题所在。EventLogRecord.Properties有时包含不同类型的
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
?
我已经研究了ProviderMetadata
及其其他属性,但没有一个与之匹配
任何关于如何解决这些占位符(甚至是占位符的名称/来源)的帮助都将不胜感激。
显示此行为的事件是5152,如下所示:
谢谢。看一看和
Network interface reports message %%16411