如何写入自定义 Windows 事件日志?
我正在尝试通过 System.Diagnostics.EventLog
,但我没有看到任何事件实际写入日志。 考虑以下代码:
// 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 示例创建日志并退出应用程序。 当然,此日志创建最终将进入设置。 后续运行尝试将消息记录到创建的事件日志中。
没有抛出任何异常。 该日志似乎已成功创建(我可以在 Windows 事件查看器中打开它)。 安全日志中没有记录任何相关内容。
没有通过 WriteEntry()
记录任何消息。 由于名称不匹配,它们也不会放置在应用程序日志中。 在这种特殊情况下,我在 Server2008 上以禁用 UAC 的管理员帐户运行。 (这是一个较大的遗留产品的一小部分,需要不幸的环境。)我做错了什么?
(我这样做是因为我想使用 EventLogTraceListener
在我的 app.config 中,它没有编写任何内容,因此这是解决该问题的一部分。)
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;
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.
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.
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?
(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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎与源名称有关。 具体来说,如果我更改源名称,我就可以成功写入。 这是由于
EventLog.Source
文档:在开发过程中,日志名称和日志源已经创建和销毁了几次,并且我可能更改了日志名称而不更改源名称。
假设我的原始源和日志是
"OldSource"
和"OldLog"
。 我通过更改代码以尝试写入"NewSource"
和"NewLog"
确定这可能是问题的根源。 当我更改两者时,我成功创建并写入“NewLog”
。 然而,当我尝试通过使用"OldSource"
创建它来写入"NewLog"
时,它失败了。我通过重新启动我的开发系统并运行完全相同的代码(使用
"NewLog"
和"OldSource"
)来确认这是问题所在,并且它有效。我想这是一次重新启动确实解决了这个问题! :)
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: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.
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.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! :)