WinAPI ReportEvent-未安​​装组件 [英] WinAPI ReportEvent - component not installed

查看:103
本文介绍了WinAPI ReportEvent-未安​​装组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个简单的功能,可以从我的应用程序登录事件查看器.但是,无论错误级别如何,每次登录时都会收到以下消息:

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:

找不到源MyAppEvents中事件ID 0的描述.引发此事件的组件未安装在本地计算机上,或者安装已损坏.您可以在本地计算机上安装或修复该组件.

The description for Event ID 0 from source MyAppEvents 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.

我不是事件日志方面的专家,实际上,这是我第一次在C ++中使用它,并且文档令人困惑和误解...

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...

这是我实现的用于封装事件日志调用的方法:

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";
}

我有一个使用WIX构建的应用程序安装程序(此安装程序创建登录事件查看器所需的密钥-应用程序的子项),并且运行顺利.但是,我不明白该消息,也不知道如何将已安装的应用程序附加到事件日志中-我什至不确定这是否是问题所在,或者它可能不是我的参数之一'm作为NULL0传递.

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. 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.

我调试时也会出现此消息(不进行安装,但手动创建了应用程序子项").

This message appears also when I debug (without installing, but with the "application subkey" manually created).

你能帮我吗?

我不能使用C ++托管代码...

I can't use C++ managed code...

推荐答案

您的日志记录代码没有任何问题.该警告消息仅表示您没有在注册表中正确注册MyAppEvents事件源.在MSDN上有记录:

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. This is documented on MSDN:

RegisterEventSource函数:

pSourceName [输入]
事件源的名称检索到. 源名称必须是Eventlog注册表项下的日志的子项.请注意,安全日志仅供系统使用.

pSourceName [in]
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. Note that the Security log is for system use only.

事件源:

事件源的结构如下:

The structure of the event sources is as follows:


HKEY_LOCAL_MACHINE
   SYSTEM
      CurrentControlSet
         Services
            EventLog
               Application
                  AppName
               Security
               System
                  DriverName
               CustomLog
                  AppName

...

每个事件源均包含信息(例如消息文件)特定于将要记录事件的软件

Each event source contains information (such as a message file) specific to the software that will be logging the events

...

应用程序可以使用应用程序日志,而无需向注册表添加新的事件源. 如果应用程序调用RegisterEventSource并传递在注册表中找不到的源名称,则事件记录服务默认情况下使用应用程序日志.但是,由于没有消息文件,因此事件查看器无法将任何事件标识符或事件类别映射到描述字符串,并且会显示错误.因此,您应该为应用程序的注册表添加唯一的事件源,并指定一个消息文件.

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. 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.

仅创建MyAppEvents子项还不够,您还必须将其指向应用程序的消息文件.如果您将事件日志类别和事件消息存储为应用程序可执行文件的资源,则子项可以将可执行文件本身注册为消息文件.

It is not enough to just create the MyAppEvents subkey, you also have to point it to the message files for your app. 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.

这篇关于WinAPI ReportEvent-未安​​装组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆