Home All Groups Group Topic Archive Search About

Trace.WriteLine(object value) - causes "System.InvalidOperationException: Collection was modified; e

Author
17 Nov 2006 10:50 PM
jc
For some reason I am getting the following exception when calling
Trace.WriteLine(object value):

"System.InvalidOperationException: Collection was modified; enumeration
operation may not execute."

if (Trace.Listeners.Count > 0)
{
  Trace.WriteLine(entry); // line 424
}

-

Here is the stack trace:

********* Exception details: **********
Unexpected exception. ---> System.InvalidOperationException: Collection
was modified; enumeration operation may not execute.
   at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()

   at System.Diagnostics.TraceInternal.WriteLine(Object value)
   at System.Diagnostics.Trace.WriteLine(Object value)
   at MyLogger.Write(LogEntry entry) in MyLogger.cs:line 424

-

If I where to instead do the following, it would work.
So this seems that something in the framework is attempting to modify
the
Trace.Listeners collection:

for (int i = 0; i < Trace.Listeners.Count; i++)
{
  TraceListener traceListener = Trace.Listeners[i];
  traceListener.WriteLine(entry);
}

AddThis Social Bookmark Button