我如何写一个自定义Windows事件日志? [英] How do I write to a custom Windows event log?

查看:437
本文介绍了我如何写一个自定义Windows事件日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过的 System.Diagnostics.EventLog ,但我没有看到真正得到写入日志的任何事件。请看下面的code:

I'm attempting to set up basic logging to the windows event log in .net via System.Diagnostics.EventLog, but I'm failing to see any events actually getting written to the log. Consider the following code:

// Elsewhere in the class
private static readonly string EventLogName = "LogName";
private static readonly string EventLogSource = "AppName";

// In the only function that does something
if (!EventLog.Exists(EventLogName))
{
    EventLog.CreateEventSource(EventLogSource, EventLogName);
    return;
}
else
{
    Trace.TraceInformation("Attempting log");

    // This doesn't write anything
    EventLog.WriteEntry(EventLogSource, 
        "StaticWriteEntry", 
        EventLogEntryType.Error);

    // Neither does this
    using (var log = new EventLog())
    {
        log.Log = EventLogName;
        log.Source = EventLogSource;
        log.WriteEntry("WriteEntry?", EventLogEntryType.Error);
    }
}
return;

第一次通过创建日志并退出应用程序,每个MSDN的样本。该日志的创建最终会走的当然是进入设置,。后续运行尝试登录消息到事件日志创建

The first time through I create the log and exit the app, per the MSDN sample. This log creation will eventually go into the setup, of course. Subsequent runs attempt to log a message to the created eventlog.

没有异常被抛出。日志似乎成功创建(我可以在Windows事件查看器打开)。有关什么都没有记录到安全日志。

No exceptions are being thrown. The log appears to be successfully created (I can open it in the Windows Event Viewer). Nothing relevant is being logged to the Security log.

没有消息通过两种 WriteEntry()记录。他们不会被放置在应用程序日志由于不匹配的名字,无论是。在这种特殊情况下,我在Server2008中与UAC禁用的管理员帐户下运行。 (这是一小片一大传统产品,需要不幸的环境。)我究竟做错了什么?

No messages are logged via either WriteEntry(). They aren't placed in the Application log due to a mismatched name, either. In this particular case, I'm running on Server2008 in an admin account with UAC disabled. (This is a small piece of a larger legacy product that requires the unfortunate environment.) What am I doing wrong?

(我这样做是因为我想用一个 EventLogTraceListener 在我的app.config,它并没有写任何东西,所以这是解决这些问题的问题的一部分。)

(I'm doing this because I want to use an EventLogTraceListener in my app.config, and it wasn't writing anything, so this is part of troubleshooting that problem.)

推荐答案

这个问题似乎涉及到的源名称。特别是,如果我改变源的名字,我可以成功地写。这是由于以下行所述的 EventLog.Source 文档:

The problem seems to be related to the source name. Specifically, if I change the source name, I can successfully write. This is due to the following line at the bottom of the the EventLog.Source docs:

如果源已经被映射到一个日志,你重新映射到一个新的日志,必须重新启动计算机使更改生效。

If a source has already been mapped to a log and you remap it to a new log, you must restart the computer for the changes to take effect.

在发展的过程中,日志名称和日志源已创建和销毁几次,我可能已经改变了LOGNAME不改变源名称。

In the course of development, the log name and log source have been created and destroyed a couple times, and I may have changed the logname without changing the source name.

假设我的原始出处和日志均OldSourceOldLog。我确定,这可能是问题的根源,通过改变code试图写入newSource的NewLog。当我改变了两个,我成功创建并写信给NewLog。当我试图用OldSource创建它写入NewLog,但是,它失败了。

Assume that my original source and log were "OldSource" and "OldLog". I determined that this may be the source of the problem by changing the code to attempt to write to "NewSource" and "NewLog". When I changed both, I successfully created and wrote to "NewLog". When I tried writing to "NewLog" by creating it with "OldSource", however, it failed.

我证实,这是由当时重新启动我的开发系统和运行完全相同的code(带NewLog问题OldSource)和它的工作。

I confirmed that this was the problem by then rebooting my development system and running the exact same code (with "NewLog" and "OldSource") and it worked.

我想这是一次重新启动的地方其实并解决它! :)

I guess this is one time where rebooting actually did fix it! :)

这篇关于我如何写一个自定义Windows事件日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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