简体   繁体   English

WinAPI ReportEvent-未安​​装组件

[英]WinAPI ReportEvent - component not installed

I've implemented a simple function to log on event viewer from my application. 我实现了一个简单的功能,可以从应用程序登录事件查看器。 However, I'm getting the following message every time I log something, regardless the error level: 但是,无论错误级别如何,每次登录时都会收到以下消息:

The description for Event ID 0 from source MyAppEvents cannot be found. 找不到源MyAppEvents中事件ID 0的描述。 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. 如果事件起源于另一台计算机,则显示信息必须与事件一起保存。

I'm not an expert on Event log, actually, this is the first time I'm using it in C++, and the documentation is confusing and misleading... 我不是事件日志的专家,实际上,这是我第一次在C ++中使用它,并且文档令人困惑和误导...

Here is the method I've implemented, to encapsulate event log calls: 这是我实现的用于封装事件日志调用的方法:

HANDLE source = NULL;

void app_log(std::string m, WORD level) {
    std::wstring msg_temp (m.begin(), m.end());
    LPCWSTR msg = msg_temp.c_str();

    std::wstring name(L"MyAppEvents");
    if (source == NULL) 
        source = RegisterEventSource(NULL, name.c_str());
    if (source) {
        if (!ReportEvent(source, level, 0, 0, NULL, 1, 0, &msg, NULL))
            std::cerr << "Error when logging";
    }
    else 
        std::cerr << "Error when logging";
}

I have an installer for my app, built with WIX (this installer creates the key needed to log on event viewer - subkey of Application) and it runs smoothly. 我有一个使用WIX构建的应用程序安装程序(此安装程序创建登录事件查看器所需的密钥-Application的子项),并且运行顺利。 However, I didn't understand that message, and also don't know how to attach my installed app to the event log - I'm actually not even sure if this is the problem, or if it is maybe one of the parameters I'm passing as NULL or 0 . 但是,我不明白该消息,也不知道如何将已安装的应用程序附加到事件日志中-我什至不确定这是否是问题所在,或者它可能不是我的参数之一传递为NULL0

This message appears also when I debug (without installing, but with the "application subkey" manually created). 我调试时也会显示此消息(无需安装,但手动创建了“应用程序子项”)。

Could you help me? 你可以帮帮我吗?

I can't use C++ managed code... 我不能使用C ++托管代码...

There is nothing wrong with your logging code. 您的日志记录代码没有任何问题。 The warning message simply means that you have not properly registered the MyAppEvents event source in the Registry. 该警告消息仅表示您没有在注册表中正确注册MyAppEvents事件源。 This is documented on MSDN: 这记录在MSDN上:

RegisterEventSource function : RegisterEventSource函数

pSourceName [in] pSourceName [输入]
The name of the event source whose handle is to be retrieved. 要获取其句柄的事件源的名称。 The source name must be a subkey of a log under the Eventlog registry key . 源名称必须是Eventlog注册表项下的日志的子项 Note that the Security log is for system use only. 请注意,安全日志仅供系统使用。

Event Sources : 事件来源

The structure of the event sources is as follows: 事件源的结构如下:

\nHKEY_LOCAL_MACHINE HKEY_LOCAL_MACHINE\n   SYSTEM 系统\n      CurrentControlSet CurrentControlSet\n         Services 服务\n            EventLog 事件日志\n               Application 应用\n                  AppName 应用名称\n               Security 安全\n               System 系统\n                  DriverName 驱动名称\n               CustomLog 自定义日志\n                  AppName 应用名称\n

... ...

Each event source contains information (such as a message file ) specific to the software that will be logging the events 每个事件源均包含特定于将记录事件的软件的信息(例如消息文件

... ...

An application can use the Application log without adding a new event source to the registry. 应用程序可以使用应用程序日志,而无需向注册表添加新的事件源。 If the application calls RegisterEventSource and passes a source name that cannot be found in the registry, the event-logging service uses the Application log by default. 如果应用程序调用RegisterEventSource并传递在注册表中找不到的源名称,则事件记录服务默认情况下使用应用程序日志。 However, because there are no message files, the Event Viewer cannot map any event identifiers or event categories to a description string, and will display an error . 但是,因为没有消息文件,所以事件查看器无法将任何事件标识符或事件类别映射到描述字符串,并且将显示错误 For this reason, you should add a unique event source to the registry for your application and specify a message file. 因此,您应该为应用程序的注册表添加唯一的事件源,并指定一个消息文件。

It is not enough to just create the MyAppEvents subkey, you also have to point it to the message files for your app. 仅创建MyAppEvents子项是不够的,您还必须将其指向应用程序的消息文件。 If you store your event log categories and event messages as resources of your app executable, the subkey can register the executable itself as the message files. 如果将事件日志类别和事件消息存储为应用程序可执行文件的资源,则子项可以将可执行文件本身注册为消息文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM