我在Windows Server 2008 R2上遇到了这个问题。
我有一个自定义应用程序日志,让我们将其命名为“MyCustomLog”,此事件日志每分钟会收到数百个条目。其中一些是警告或错误。我创建了简单的控制台应用程序来打印事件:
class Program
{
static void Main(string[] args)
{
EventLog eventLog = new EventLog("MyCustomLog", Environment.MachineName);
eventLog.EntryWritten += new EntryWrittenEventHandler(OnEntryWritten);
eventLog.EnableRaisingEvents = true;
Console.ReadLine();
}
private static void OnEntryWritten(object sender, EntryWrittenEventArgs e)
{
if (e.Entry.EntryType == EventLogEntryType.Error
|| e.Entry.EntryType == EventLogEntryType.Warning)
{
Console.WriteLine();
Console.WriteLine("Now Date:" + DateTime.Now);
Console.WriteLine("Received:" + e.Entry.TimeWritten);
Console.WriteLine(e.Entry.Message);
}
}
}
EntryWritten
不时处理过去的事件,我指的是前一天左右的事件......
为什么会这样?我错过了什么?
更新:以下是应用输出的示例:
Now Date:2013-10-02 15:05:04
Received:2013-10-01 10:20:02
<Some Data>
Now Date:2013-10-02 15:05:04
Received:2013-10-01 10:20:08
<Some Data>
Now Date:2013-10-02 15:05:04
Received:2013-10-01 10:20:49
<Some Data>
Now Date:2013-10-02 15:05:04
Received:2013-10-01 10:20:54
<Some Data>
答案 0 :(得分:-1)
在OnEntryWritten函数中,'e'实际上不包含当前日志,而是EntryWrittenEventArgs类的实例。你必须找到一种获取当前条目索引的方法..易于检索 - &gt;项[Entries.Count-1]。这将包含您当前的日志。希望这会有所帮助。