简体   繁体   English

Windows窗体事件日志

[英]Windows Forms Event Log

I am writing to the event log from my Windows Forms application running on Windows 7 and am getting this message in the event log : 我从Windows 7上运行的Windows窗体应用程序写入事件日志,并在事件日志中收到此消息:

The description for Event ID X from source Application cannot be found. 无法找到源应用程序中的事件ID X的描述。 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: 活动中包含以下信息:

Exception Details 例外细节

the message resource is present but the message is not found in the string/message table 消息资源存在但在字符串/消息表中找不到该消息

My logging code is: 我的日志代码是:

public void Log(Exception exc)
{
    EventLog.WriteEntry(
        "Application", 
        exc.ToString(), 
        EventLogEntryType.Error, 
        100);
}

My logging on Windows Forms is usually to a DB, but in this case decided to use the event log. 我在Windows窗体上的日志记录通常是数据库,但在这种情况下决定使用事件日志。 I usually use the event log in ASP.NET applications, but those are on XP Pro locally and Windows Server 2003 on the web boxes. 我通常在ASP.NET应用程序中使用事件日志,但这些是在本地XP Pro和Web服务器上的Windows Server 2003上。

Is this a Windows 7 thing or a Windows Forms thing, and what should I do to fix this? 这是Windows 7的东西还是Windows Forms的东西,我该怎么做才能解决这个问题? Thanks. 谢谢。

请参阅http://support.microsoft.com/kb/307024 ,具体而言,请不要忘记创建事件源。

This solution was working for me: 这个解决方案对我有用:

In the registry of windows 7 or Win 2008 R2 there is a key HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\eventlog\\CustomEventLog under this key are all the registered valid sources for that event log. 在Windows 7或Win 2008 R2的注册表中,此键下的键HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ services \\ eventlog \\ CustomEventLog是该事件日志的所有已注册有效源。

When you try and write to an Event log with a source that isn't a valid (isn't a key under HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\eventlog\\CustomEventLog) it enumerates the other Event logs keys to see if there source exists there. 当您尝试使用非有效的源(不是HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ services \\ eventlog \\ CustomEventLog下的键)写入事件日志时,它会枚举其他事件日志键以查看是否存在源那里。

I added all missing sources under HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\eventlog\\CustomEventLog key. 我在HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ services \\ eventlog \\ CustomEventLog键下添加了所有缺少的源。 Just create a copy of existing keys under this key and rename it to your EventSource. 只需在此密钥下创建现有密钥的副本,并将其重命名为EventSource。

The first parameter in this overload is "The source by which the application is registered on the specified computer." 此重载中的第一个参数是“应用程序在指定计算机上注册的源”。 documented here If this source is, as in your case, "Application" you get this behavior. 此处记录如果此源是“应用程序”,则会出现此行为。 (Could it be that you mistakingly think the 1st parameter refers to the Windows Log : Application, Security, etc ?) (可能是你错误地认为第一个参数是指Windows日志:应用程序,安全性等?)

To register your source do this : 注册您的来源做到这一点:

public void Log(Exception exc){
    if(!EventLog.SourceExists("MySource"))
    {
        EventLog.CreateEventSource("MySource", "MyNewLog");
        return ;
    }
    EventLog.WriteEntry(
     "MySource", 
     exc.ToString(), 
     EventLogEntryType.Error, 
     100); }  

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

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