Home All Groups Group Topic Archive Search About

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

Author
17 Nov 2006 10:52 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);
}

Author
18 Nov 2006 5:37 AM
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.
-----------------------------------------
Show quote
"jc" <johncarne***@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);
> }
>
Author
20 Nov 2006 8:03 AM
Laurent Bugnion
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
Author
20 Nov 2006 2:17 PM
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
{
  .
  .
}
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:
Show quote
> 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

AddThis Social Bookmark Button