I currently have a project that takes every x number of seconds (using Timer and a 10000 milliseconds Interval) the events happening on Windows and filters them under certain conditions. Each time the Timer does Tick, date and hour corresponding to 10 seconds before (due to the Interval) the moment events are checked, and the next method is executed:
// 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;
}
That event obtains a list of all events (obtained by using EventLog.Entries) and the date and time an event has to have to get added to the filtered events list (so an event had to be generated 10 seconds ago to be 'accepted').
But, during eventsList iterating, an IndexOutOfRangeException
is generated being i on the first test around 28000 and on the second test 43, and the Count property around 31000 in both tests.
¿Is anyone able to tell me why this happens?
Here is a screenshot of the exception data (it is in Spanish, sorry): IndexOutOfRange exception data