我正在尝试从Windows evens日志中提取给定级别的最后N个事件。在MSDN页面(http://msdn.microsoft.com/en-us/library/bb671200.aspx)之后,我提出了以下解决方案(可行):
List<string> records = new List<string>();
string queryString = string.Format("*[System/Level={0}]", level);
EventLogQuery eventsQuery = new EventLogQuery(eventLogLocation, PathType.FilePath, queryString);
eventsQuery.ReverseDirection = true;
var reader = new EventLogReader(eventsQuery);
var record = reader.ReadEvent();
while (record != null && numRecords > 0)
{
records.Add(record.FormatDescription());
record = reader.ReadEvent();
}
是否可以在循环中查询给定数量的事件?