我刚才写一些代码来帮我出这一点。源注册在另一个日志问题,我遇到过,不想手动必须从日志中删除源。我决定要做的是检查源是否存在,是否检查链接到正确的日志,是否不删除源,现在它不存在或者它从未创建过Log品牌新。
protected const string EventLogName = "MyLog";
private static bool CheckSourceExists(string source) {
if (EventLog.SourceExists(source)) {
EventLog evLog = new EventLog {Source = source};
if (evLog.Log != EventLogName) {
EventLog.DeleteEventSource(source);
}
}
if (!EventLog.SourceExists(source)) {
EventLog.CreateEventSource(source, EventLogName);
EventLog.WriteEntry(source, String.Format("Event Log Created '{0}'/'{1}'", EventLogName, source), EventLogEntryType.Information);
}
return EventLog.SourceExists(source);
}
public static void WriteEventToMyLog(string source, string text, EventLogEntryType type) {
if (CheckSourceExists(source)) {
EventLog.WriteEntry(source, text, type);
}
}
希望这有助于:)
这基本上是我的问题。 Windows事件日志有点棘手,因为如果这是您第一次写入新的自定义日志,直到重新启动计算机,它可能无法正常工作。我让它创建了日志,但它仍然将消息写入新的自定义日志和事件查看器中的应用程序日志,直到我重新启动机器。之后,新消息刚刚记录到我的新自定义日志中。谢谢! – Jez 2009-12-14 15:49:55
经过一段时间的调试,我重新启动。这工作! – jonas 2014-04-28 12:18:12
重新启动EventViewer帮助我在Win8上的情况。 – Legends 2015-04-25 23:52:38