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

Re: Trace.WriteLine(object value) - causes "System.InvalidOperationException: Collection was modified; enumeration operation may not execute." by RobinS

RobinS
Fri Nov 17 23:37:40 CST 2006

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.
-----------------------------------------
"jc" <johncarneiro@verizon.net> wrote in message
news:1163803927.529382.222510@f16g2000cwb.googlegroups.com...
> 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);
> }
>



Re: Trace.WriteLine(object value) - causes "System.InvalidOperationException: by Laurent

Laurent
Mon Nov 20 02:03:18 CST 2006

Hi,

RobinS wrote:
> 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.

Every Object has a ToString() method, because it is defined in the
Object class. The only thing you can do is override it.
http://msdn2.microsoft.com/en-us/library/system.object.tostring(VS.80).aspx

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Re: Trace.WriteLine(object value) - causes "System.InvalidOperationException: Collection was modified; enumeration operation may not execute." by jc

jc
Mon Nov 20 08:17:10 CST 2006

I found the issue:

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

try
{
.
.
}
catch
{
Trace.Listeners.Remove(sinkName);
.
.
}

It turns out that when running in unit test mode, at times we got the
exception
"System.ObjectDisposedException: Cannot write to a closed TextWriter"
and it would enter the catch block and in turn cause the exception
"System.InvalidOperationException: Collection was modified; enumeration
operation may not execute"

We are now investigating why the "System.ObjectDisposedException:
Cannot write to a closed TextWriter" exception if happening in the unit
test, but the workaround 'for-loop' vs 'enumerating' seems to be
working for the moment.

Laurent Bugnion wrote:
> Hi,
>
> RobinS wrote:
> > 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.
>
> Every Object has a ToString() method, because it is defined in the
> Object class. The only thing you can do is override it.
> http://msdn2.microsoft.com/en-us/library/system.object.tostring(VS.80).aspx
>
> HTH,
> Laurent
> --
> Laurent Bugnion, GalaSoft
> Software engineering: http://www.galasoft-LB.ch
> Private/Malaysia: http://mypage.bluewin.ch/lbugnion
> Support children in Calcutta: http://www.calcutta-espoir.ch