【问题标题】:The description string for parameter reference (%1) could not be found找不到参数引用 (%1) 的描述字符串
【发布时间】:2018-06-12 17:36:45
【问题描述】:

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

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

【问题讨论】:

  • 我无法控制的字符串在 EventRecord 中,但它不允许我访问原始字符串。我检查了它是 propertiesmethods,但我还没有找到可以使用的东西。
  • 我检查了 Windows 事件查看器的功能,它将%%0 之类的模式替换为一些预定义的字符串,但只留下%%4294967295 之类的未知代码。相比之下,PowerShell 中的 Get-WinEvent 根本不尝试任何替换(甚至对于 %%0 也没有)。

标签: c# .net windows event-log


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 2019-07-03
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多