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

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);
}
 
R

RobinS

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.
 
J

jc

I found the issue:

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

try
{
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top