从事件日志类的文档中,EventLogEntryCollection是事件日志条目的动态列表。它建议直接使用Count属性,而不是将其值存储在变量中。实现此行为:
private void ReadEventLog()
{
EventLog eventLog = new EventLog("Application", "TheGreatestMachineInTheWorld");
EventLogEntryCollection eventLogEntries = eventLog.Entries;
for (int i = 0; i < eventLogEntries.Count; i++){
EventLogEntry entry = eventLog.Entries[i];
//Do Some processing on the entry
}对于大型事件日志( >20000项)来说速度较慢。for循环而不是foreach的原因是因为我需要迭代器位置来指示这件事离完成有多近。
存储count变量并对其进行迭代:
int eventLogEntryCount = eventLogEntries.Count;
for (int i = 0; i < eventLogEntryCount; i++){
EventLogEntry entry = eventLog.Entries[i];
//Do Some processing on the entry
}提供了显著的性能提升。但是,当处理发生时,如果事件日志被写入,则会打开索引超出范围异常的可能性。是否有一种静态存储此列表的方法,以便计数不发生更改?
发布于 2014-05-21 15:33:18
是否有一种静态存储此列表的方法,以便计数不发生更改?
听起来你想要的是:
List<EventLogEntry> entries = eventLogEntries.Cast<EventLogEntry>().ToList();这将将所有日志条目(当时)提取到一个List<T>中,然后该日志条目将快速地使用索引器或另一个foreach进行访问。
当然,您可能会发现只获取所有日志条目是很慢的--您应该对此进行实验。
https://stackoverflow.com/questions/23787672
复制相似问题