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

The description string for parameter reference (%1) could not be found

我在尝试使用 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)

当事件的文本包含字符串 %% 后跟一个长数字时会发生异常(来自我无法控制的源的某些事件包含该模式)。那些 %% 只是文本,我不希望那时 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 不一定包含描述字符串,有时这些事件有一个参数列表,用于填充消息模板以生成描述字符串,所以在这种情况下你会得到原始参数.