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

  • Thread starter Thread starter jc
  • Start date Start date
J

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;
traceListener.WriteLine(entry);
}
 
Does the the object called "entry" have a ToString
method? Just for grins, try Trace.WriteLine(entry.ToString);
and see what that does.

Robin S.
 
I found the issue:

In the TraceListener WriteLine implementation, we
had a try/catch block to intercept all exceptions and do the following.

try
{
 
Back
Top