Iterating through Event Log Entry Collection, IndexOutOutOfBoundsException

Posted by fjdumont on Stack Overflow See other posts from Stack Overflow or by fjdumont
Published on 2011-01-12T13:16:18Z Indexed on 2011/01/12 13:54 UTC
Read the original article Hit count: 248

Hello,

in a service application I am iterating through the Windows application event log to parse Events in order react depanding on the entry message. In the case that the event log is full (Windows usually makes sure there is enough space by deleting old entries - this is configurable in the eventvwr.exe settings), the service always runs into an IndexOutOfBoundsException while iterating through the EventLog.Entries collection. No matter how I iterate (for-loop, using the collections enumerator, copying the collection into an array, ...), I can't seem to get rid of this ´bug´.

Currently, I ensure that the log is not full in order to keep the service running by regularly deleting the last few item by parsing the event log file and deleting the last few nodes (Don't beat me up, I couldn't find a better alternative...).

How can I iterate through the collection without trying to access already deleted entries?

Is there probably a more elegant method? I am only trying to acces the logs written during the last x seconds (even LINQ failed to select those when the log is full - same exception), could this help?

Thanks for any advice and hints

Frank

Edit: I forgot to mention that my assumption is the loops are accessing entries which are being deleted during iteration by Windows. Basically that is why I tried to clone the collection. Is there perhaps a way to lock the collection for a small amount of time for just my application?

© Stack Overflow or respective owner

Related posts about c#

Related posts about collections