C# EventLog.EntryWrited事件处理过去的事件
我在Windows Server 2008 R2上遇到此问题 我有一个自定义的应用程序日志,让我们把它命名为“MyCustomLog”,这个事件日志每分钟接收数百条条目。其中一些是警告或错误。我已经创建了用于打印事件的简单控制台应用程序:C# EventLog.EntryWrited事件处理过去的事件,c#,.net,windows,events,C#,.net,Windows,Events,我在Windows Server 2008 R2上遇到此问题 我有一个自定义的应用程序日志,让我们把它命名为“MyCustomLog”,这个事件日志每分钟接收数百条条目。其中一些是警告或错误。我已经创建了用于打印事件的简单控制台应用程序: class Program { static void Main(string[] args) { EventLog eventLog = new EventLog("MyCustomLog", Environment.Mach
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);
}
}
}
不时地entrywrited
处理过去的事件,我指的是前一天左右的事件
为什么会这样?我错过了什么
更新:以下是一个应用程序输出示例:
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>
现在日期:2013-10-02 15:05:04
收到日期:2013-10-01 10:20:02
现在日期:2013-10-02 15:05:04
收到日期:2013-10-01 10:20:08
现在日期:2013-10-02 15:05:04
收到日期:2013-10-01 10:20:49
现在日期:2013-10-02 15:05:04
收到日期:2013-10-01 10:20:54
在onentrywrited函数中,您的“e”实际上并不包含当前日志,而是EntryWrittenEventArgs类的实例。您必须找到一种获取当前条目索引的方法。。易于检索->条目[Entries.Count-1]。这将有您当前的日志。希望这有帮助