在 for 循环中遍历 EventLogEntryCollection 时出现 IndexOutOfRangeException
IndexOutOfRangeException when iterating through a EventLogEntryCollection in a for loop
我目前有一个项目,每隔 x 秒(使用计时器和 10000 毫秒间隔)记录 Windows 上发生的事件,并在特定条件下过滤它们。
Timer每次做Tick,date和hour对应的10秒前(由于Interval)检查moment events,然后执行下一个方法:
// Method that returns wanted events from an EventLogEntryCollection
private List<EventLogEntry> lastEvents(EventLogEntryCollection eventsList, DateTime minimumMoment)
{
// Wanted events list
// 4624: Login
// 4634: Logout
long[] events = new long[] { 4624, 4634 };
// Table to return with valid events
List<EventLogEntry> filteredEventsList = new List<EventLogEntry>();
// Checking if there are events to filter
if (eventsList.Count > 0)
{
// There are events to filter
// Going across all events to filter
for (int i = 0; i < eventsList.Count; i++)
{
// Getting the current event
EventLogEntry event = eventsList[i]; // Line with exception
// Checking if the current event happened in the last 10 seconds (due to the Interval)
if (event.TimeGenerated >= minimumMoment)
{
// The event is valid time wise
// Checking if the event's ID is contained in the required ID's list
if (events.Contains(event.InstanceId))
{
// The event has a valid ID
// Adding the event to the list
filteredEventsList.Add(event);
}
}
}
}
// Returning obtained list
return filteredEventsList;
}
该事件获取所有事件的列表(通过使用 EventLog.Entries 获取)以及事件必须添加到过滤事件列表中的日期和时间(因此必须生成一个事件 10秒前为 'accepted')。
但是,在 eventsList 迭代期间,在第一次测试中生成了一个 IndexOutOfRangeException
,在 28000 左右,在第二次测试中生成了 43,并且在这两个测试中生成了 Count 属性 在 31000 左右。
¿有人能告诉我为什么会这样吗?
这是异常数据的屏幕截图(对不起,它是西班牙语):
IndexOutOfRange exception data
根据the docs:
EventLogEntry objects are indexed by the event log system according to
the chronological order in which they arrived in the event log. Use
the Item[Int32] property to select a specific event log entry whose
index in the collection is known.
Iterating through the EventLogEntryCollection instance steps through
each EventLogEntry object sequentially. The collection is dynamic and
the number of entries may not be immutable when you enter the loop.
Therefore, you should use a for each...next loop instead of a for loop to step through entries that are associated
with the EventLogEntryCollection instance to examine the entire set of
entries.
Because new entries are appended to the existing list, stepping
through the collection enables you to access the entries that were
created after you originally created the EventLogEntryCollection.
此外,Count 的文档,状态:
An EventLogEntryCollection represents a dynamic list of all the
entries in a log. Therefore, the Count property can change during the
lifetime of the EventLogEntryCollection instance that you create. It
is usually best to work with the Count property directly instead of
assigning its value to a variable.
因此,简而言之,Count
正在发生变化(可能会减少)- 导致您查找不再存在的索引。使用 foreach
而不是 for
将为您解决这个问题。
我目前有一个项目,每隔 x 秒(使用计时器和 10000 毫秒间隔)记录 Windows 上发生的事件,并在特定条件下过滤它们。 Timer每次做Tick,date和hour对应的10秒前(由于Interval)检查moment events,然后执行下一个方法:
// Method that returns wanted events from an EventLogEntryCollection
private List<EventLogEntry> lastEvents(EventLogEntryCollection eventsList, DateTime minimumMoment)
{
// Wanted events list
// 4624: Login
// 4634: Logout
long[] events = new long[] { 4624, 4634 };
// Table to return with valid events
List<EventLogEntry> filteredEventsList = new List<EventLogEntry>();
// Checking if there are events to filter
if (eventsList.Count > 0)
{
// There are events to filter
// Going across all events to filter
for (int i = 0; i < eventsList.Count; i++)
{
// Getting the current event
EventLogEntry event = eventsList[i]; // Line with exception
// Checking if the current event happened in the last 10 seconds (due to the Interval)
if (event.TimeGenerated >= minimumMoment)
{
// The event is valid time wise
// Checking if the event's ID is contained in the required ID's list
if (events.Contains(event.InstanceId))
{
// The event has a valid ID
// Adding the event to the list
filteredEventsList.Add(event);
}
}
}
}
// Returning obtained list
return filteredEventsList;
}
该事件获取所有事件的列表(通过使用 EventLog.Entries 获取)以及事件必须添加到过滤事件列表中的日期和时间(因此必须生成一个事件 10秒前为 'accepted')。
但是,在 eventsList 迭代期间,在第一次测试中生成了一个 IndexOutOfRangeException
,在 28000 左右,在第二次测试中生成了 43,并且在这两个测试中生成了 Count 属性 在 31000 左右。
¿有人能告诉我为什么会这样吗?
这是异常数据的屏幕截图(对不起,它是西班牙语): IndexOutOfRange exception data
根据the docs:
EventLogEntry objects are indexed by the event log system according to the chronological order in which they arrived in the event log. Use the Item[Int32] property to select a specific event log entry whose index in the collection is known.
Iterating through the EventLogEntryCollection instance steps through each EventLogEntry object sequentially. The collection is dynamic and the number of entries may not be immutable when you enter the loop. Therefore, you should use a for each...next loop instead of a for loop to step through entries that are associated with the EventLogEntryCollection instance to examine the entire set of entries.
Because new entries are appended to the existing list, stepping through the collection enables you to access the entries that were created after you originally created the EventLogEntryCollection.
此外,Count 的文档,状态:
An EventLogEntryCollection represents a dynamic list of all the entries in a log. Therefore, the Count property can change during the lifetime of the EventLogEntryCollection instance that you create. It is usually best to work with the Count property directly instead of assigning its value to a variable.
因此,简而言之,Count
正在发生变化(可能会减少)- 导致您查找不再存在的索引。使用 foreach
而不是 for
将为您解决这个问题。