Trace.WriteLine really really slow? or just me?

Is it just me or is trace.WriteLine() really eally really really slow I used to have a class that was just a multi-line textbox that I would append a message to whenever calling a method i made called WriteDebug()... but blindly I switched to Trace.WriteLines without first making sure it was a reasonable way to do it.. my app has slowed down over 1000%!   I'm not doing anything special with the trace.. i don't understand why its so slow. anyone else noticing this

<trace autoflush="false" indentsize="4">

<listeners>
   
<add name="ConsoleLog" type="System.Diagnostics.ConsoleTraceListener" />
</
listeners>



Answer this question

Trace.WriteLine really really slow? or just me?

  • bilalso

    It's writing to the console that's slow, not Trace.WriteLine.

    I wrote a small sample and found that this:

      Trace.Listeners.Add(new ConsoleTraceListener());

      Stopwatch sw = new Stopwatch();
      sw.Start();
      for(int i = 0; i < 1000; i++)
      {
       Console.WriteLine(";");
      }
      sw.Stop();
      Console.WriteLine(sw.Elapsed);

    ran in 2.07 seconds and the same program, tracing after every Console.WriteLine took 4.16 seconds, so Trace.WriteLine with a console listener only takes as long as Console.WriteLine.

    More interestingly, changing the console trace listener to a text writer trace listener reduced the time to 2.09 seconds.

    So the problems the listener, not Trace.WriteLine. Try a different listener (TextWriterTraceListener, for example), and your problem should be solved.


  • Trace.WriteLine really really slow? or just me?