【问题标题】:Windows Event Viewer shows wrong severity for all messages from my serviceWindows 事件查看器显示来自我的服务的所有消息的严重性错误
【发布时间】:2020-11-15 17:10:14
【问题描述】:

我正在尝试根据tutorial 在 C 中创建自己的 Windows 服务。我的事件消息定义文件如下所示

EventLogMessages.mc:

MessageIdTypedef=DWORD

SeverityNames=(
    Success=0x0:STATUS_SEVERITY_SUCCESS
    Info=0x1:STATUS_SEVERITY_INFO
    Warning=0x2:STATUS_SEVERITY_WARNING
    Error=0x3:STATUS_SEVERITY_ERROR
)

FacilityNames=(
    System=0x0:FACILITY_SYSTEM
    Runtime=0x2:FACILITY_RUNTIME
    Stubs=0x3:FACILITY_STUBS
    Io=0x4:FACILITY_IO_ERROR_CODE
)

LanguageNames=(English=0x409:MSG00409)

; // The following are message definitions.

MessageId=0x1
Severity=Success
Facility=Runtime
SymbolicName=SVCEVENT_STATUS_REPORT
Language=English
Status report: %2.
.

MessageId=0x2
Severity=Error
Facility=System
SymbolicName=SVCEVENT_INIT_SYSCALL_ERROR
Language=English
Essential syscall failed when starting the service: %2.
.

MessageId=0x3
Severity=Error
Facility=Runtime
SymbolicName=SVCEVENT_CUSTOM_ERROR
Language=English
Service-specific error: %2.
.

; // A message file must end with a period on its own line
; // followed by a blank line.

这几乎是来自docs的示例文件的副本。

但是,每当我编译此文件、启动服务并打开事件查看器时,我都会将来自我的服务的所有消息都视为“级别:错误”,即使是那些应该具有较低严重性的消息。

事件 ID 和消息是正确的,但严重性总是错误的。

我打开了生成的头文件EventLogMessages.h,这里好像也没问题。

#define SVCEVENT_STATUS_REPORT           ((DWORD)0x00020001L)

高 2 位(表示严重性)正确设置为 0 - STATUS_SEVERITY_SUCCESS。 但无论如何,事件总是显示错误。

有人知道为什么会出错以及如何解决吗?

【问题讨论】:

    标签: c windows service event-log


    【解决方案1】:

    所以我通过反复试验弄明白了。

    严重性实际上仅由ReportEvent 的第二个参数控制,messageID 的高位(来自 .mc 文件并转到第四个参数)被完全忽略。

    所以要发送“警告”事件,而不是像定义消息属性

    MessageId=0x2
    Severity=Warning
    Facility=Runtime
    SymbolicName=SVCEVENT_WARNING
    ...
    

    你需要这样调用函数

    ReportEvent( hEventSource, EVENTLOG_WARNING_TYPE, 0, SVCEVENT_WARNING, ... );
    

    不确定哪种消息严重性适用于那时,但它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      • 1970-01-01
      • 2023-03-11
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多