下面的代码在Win7机器上可以正常运行,但是当我在Win10上运行它时,它仅显示前3个错误并停止
string machine = ".";
var aLog = new EventLog("system", machine);
List <EventLogEntry> entries = (from entry in aLog.Entries.Cast<EventLogEntry>()
where entry.TimeWritten >= DateTime.Today &&
entry.EntryType == EventLogEntryType.Error
orderby entry.TimeWritten descending
select entry).ToList();
Console.WriteLine("COUNT= " +entries.Count); //This is 3 but there is 10 system errors
foreach(var entry in entries)
{
Console.WriteLine(entry.Source);
Console.WriteLine(entry.Message);
Console.WriteLine();
}
答案 0 :(得分:1)
代码完全按照您的要求进行操作,检索日期大于今天或从今天开始的所有内容。屏幕快照中过滤的视图是过去24小时内发生的所有事情,这是不一样的。
如果要使用该视图,请更改where子句。
var twentyFourHoursAgo = DateTime.Now.AddHours(-24); // not sure if this should be UTC or local, change to UtcNow if applicable
List <EventLogEntry> entries = (from entry in aLog.Entries.Cast<EventLogEntry>()
where entry.TimeWritten >= twentyFourHoursAgo &&
entry.EntryType == EventLogEntryType.Error
orderby entry.TimeWritten descending
select entry).ToList();