tags:

views:

5500

answers:

4

I want to introduce some tracing to a C# application I am writing. Sadly, I can never really remember how it works and would like a tutorial with reference qualities to check up on every now and then. It should include:

  • App.config / Web.config stuff to add for registering TraceListeners
  • how to set it up in the calling application

Do you know the uber tutorial that we should link to?

EDIT: Glenn Slaven pointed me in the right direction. Add this to your App.config/Web.config inside <configuration/>:

<system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add type="System.Diagnostics.TextWriterTraceListener" name="TextWriter"
             initializeData="trace.log" />
      </listeners>
    </trace>
</system.diagnostics>

This will add a TextWriterTraceListener that will catch everything you send to with Trace.WriteLine etc.

Tip: If you don't add any listeners, then you can still see the trace output with the SysInternals program DebugView (Dbgview.exe): http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx

+1  A: 

Don't know if this link will help you?

http://www.codeguru.com/csharp/.net/net_debugging/tracing/article.php/c5919/

Mark Ingram
+1  A: 

DotNetCoders has a starter article on it: http://www.dotnetcoders.com/web/Articles/ShowArticle.aspx?article=50, they talk about how to set up the switches in the config file & how to write the code, but it is pretty old (2002). There's another article on CodeProject: http://www.codeproject.com/KB/trace/debugtreatise.aspx but it's the same age. CodeGuru has another article on custom TraceListeners: http://www.codeguru.com/columns/vb/article.php/c5611

I can't think of any more recent articles, hopefully someone else here will have something

Glenn Slaven
+3  A: 

I just found one more, this one seems to have a lot of detail about setting up the listeners in the config file: http://olondono.blogspot.com/2008/01/about-trace-listeners.html

Glenn Slaven
+4  A: 

I wrote a short article on using the Trace Listener - maybe it will be of some help, especially for beginners - http://www.daveoncsharp.com/2009/09/create-a-logger-using-the-trace-listener-in-csharp/

David Azzopardi