【问题标题】:Event log message file issue事件日志消息文件问题
【发布时间】:2012-02-27 17:14:49
【问题描述】:

我工作的一个应用程序一直在吐出丑陋的事件日志消息,其中包含我们的消息,但也有如下精彩消息:

The description for Event ID 103 from source MyCustomSource cannot be found. Either  the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: 

My event log message that is redacted.

the message resource is present but the message is not found in the string/message table

所以我开始为这个源创建一个事件日志消息文件,听起来很简单吧?

;// Header
MessageIdTypedef=DWORD

LanguageNames=(
    English=0x409:MSG00409
)

;// Categories
MessageId=0x1
SymbolicName=MYAPP_CATEGORY_GENERAL
Language=English
MyApp General
.

;// Messages
MessageId=0x103
SymbolicName=API_ERROR
Severity=Error
Language=English
An error occurred in the API. Message: %1
.

然后我照常编译这个文件:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\mc.exe" -u MyAppMessages.mc"
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\rc.exe" -r MyAppMessages.rc"
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\link.exe" -dll -noentry -out:MyAppMessages.dll MyAppMessages.res /MACHINE:x86

我现在有了已编译的 MyAppMessages.dll。我现在添加所需的注册表项:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\MyApp\MyApp
CategoryCount    REG_DWORD    1
CategoryMessageFile REG_EXPAND_SZ <path to MyAppMessages.dll>
EventMessageFile REG_EXPAND_SZ <path to MyAppMessages.dll>

问题是,我仍然收到与开始时相同的消息,只有任务类别现在从消息文件中加载正确的值,而不是之前加载的默认值 (1)。

这是事件数据的 XML:

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
  <System>
    <Provider Name="MyApp" /> 
    <EventID Qualifiers="57344">103</EventID> 
    <Level>2</Level> 
    <Task>1</Task> 
    <Keywords>0x80000000000000</Keywords> 
    <TimeCreated SystemTime="2012-02-27T16:42:20.000000000Z" /> 
    <EventRecordID>20759</EventRecordID> 
    <Channel>MyApp</Channel> 
    <Computer>Skycaller</Computer> 
    <Security /> 
  </System>
  <EventData>
    <Data>My event log message that is redacted.</Data> 
  </EventData>
</Event>

我不是消息文件专家,但它在消息文件中查找类别定义,而不是在事件消息中查找。有没有人知道为什么找不到消息但在同一个 DLL 中找到了类别?

【问题讨论】:

  • 编辑:作为进一步的信息,使用 Resource Hacker 我可以打开已编译的资源文件,其中包含所有消息。
  • 用 SDK 7.1 而不是 7.0A 编译似乎也没什么区别。

标签: .net message event-log


【解决方案1】:

事实证明,MSDN 论坛上的某个人无意中发现了此问题的解决方案并与我分享。

只需删除消息文件中带有Severity=xxxxxFacility=xxxxx 的任何行,然后重新编译后就会显示自定义消息。 Facility 不在我的档案中,但另一个人在他的文件中有那条线,如果不把那条线也拿出来,他就不会工作。不知道为什么这些行出现在很多教程和官方 MSDN 文档示例中,但确实存在。

希望这对某人有所帮助!

【讨论】:

    【解决方案2】:

    您将 MessageId 值定义为十六进制,因此 0x103 转换为十进制 259。如果您希望 MessageId 为十进制 103,请使用 MessageId=0x67

    【讨论】:

    • 我试过 MessageId 值 103(最明显)、0x103(匹配 MSDN 上的一些文档)以及 0x67,正如您所说,正确的十六进制值 103d。它们都没有被事件日志拾取。 :( 类别以十进制或十六进制工作,奇怪的是消息不起作用!
    【解决方案3】:

    EventId 是 MessageId、Severity 和 Facility 的组合。我从以下来源阅读:“指定的任何值都必须适合 16 位。有关如何从严重性、设施和消息 ID 形成消息值的更多详细信息,请参阅 Winerror.h 中的图表。” http://msdn.microsoft.com/en-us/library/windows/desktop/dd996906%28v=vs.85%29.aspx

    这就是为什么如果您将 Severity 和 Facility 从消息文件中取出,它会起作用。

    【讨论】:

      【解决方案4】:

      传递给 Win32 ReportEvent 调用的 dwEventId 参数与消息文件中的 MessageId 并不完全相同;相反,它应该由SeverityFacilityMessageId 组合而成;如winerror.h 所述,进行了位移和或运算。

      除了 RC 和 BIN 文件之外,消息编译器还为每条消息生成一个从 SymbolicName(如果提供)到正确 EventId 的映射的头文件,该文件由 #defines 提供。因此,如果您想在 Windows 事件日志消息中使用 SeverityFacility,则可以只使用这些常量。

      请注意,如果您确实为任何消息指定了 FacilitySeverity 的值,那么对于未定义 Facility 或 @987654337 的任何后续消息,消息编译器将假定该值是默认值@,直到到达指定 FacilitySeverity 的值的消息,该值成为新的默认值,等等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-04
        • 1970-01-01
        • 1970-01-01
        • 2015-01-06
        • 2014-02-21
        • 1970-01-01
        • 2018-02-10
        • 1970-01-01
        相关资源
        最近更新 更多