gpt4 book ai didi

c# - 找不到参数引用 (%1) 的描述字符串

转载 作者:行者123 更新时间:2023-12-02 01:00:37 24 4
gpt4 key购买 nike

我在尝试使用 C# 的方法从 Windows 日志中读取时遇到此异常 EventRecord.FormatDescription() :

System.Diagnostics.Eventing.Reader.EventLogException: The description string for parameter reference (%1) could not be found
at System.Diagnostics.Eventing.Reader.EventLogException.Throw(Int32 errorCode)
at System.Diagnostics.Eventing.Reader.NativeWrapper.EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLogHandle eventHandle, EvtFormatMessageFlags flag)
at System.Diagnostics.Eventing.Reader.ProviderMetadataCachedInformation.GetFormatDescription(String ProviderName, EventLogHandle eventHandle)

当事件的文本包含字符串 %% 后跟一个长数字时会发生异常(来 self 无法控制的源的某些事件包含该模式)。那些 %% 只是文本,我不希望此时 Windows 有任何解析智能。

当事件的文本包含该模式时,您知道我可以做什么来避免 .Net 抛出此错误吗?

以下是下次您尝试从 C# 程序读取事件时会导致异常的 PowerShell 命令:

New-EventLog -LogName Application -Source MyApp
Write-EventLog -Source MyApp -LogName Application -Message "%%4294967295" -EventId 3

最佳答案

我最终实现的解决方法如下:

    private string FormatEventDescription(EventRecord eventRecord)
{
try
{
return eventRecord.FormatDescription();
}
catch (EventLogException e)
{
return eventRecord.ToXml();
}
}

这并不令人满意,因为 XML 不是用户友好的,但至少它具有我们需要了解 EventRecord 的原始内容时所需的所有信息。请注意,XML 内部不一定包含描述字符串,有时这些事件有一个参数列表,用于填充消息模板以生成描述字符串,因此在这种情况下,您将获得原始参数。

关于c# - 找不到参数引用 (%1) 的描述字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50822808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com