123

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 über tutorial that we should link to?


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.

@DanEsparza pointed out that you should use Trace.TraceInformation, Trace.TraceWarning and Trace.TraceError instead of Trace.WriteLine, as they allow you to format messages the same way as string.Format.

Tip: If you don't add any listeners, then you can still see the trace output with the Sysinternals program DebugView (Dbgview.exe):

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • 4
    I highly recommend using `Trace.TraceInformation` and the like instead of WriteLine. Those members allow you to format your messages like `string.Format`. – Dan Esparza Mar 05 '14 at 15:54

2 Answers2

5

I followed around five different answers as well as all the blog posts in the previous answers and still had problems. I was trying to add a listener to some existing code that was tracing using the TraceSource.TraceEvent(TraceEventType, Int32, String) method where the TraceSource object was initialised with a string making it a 'named source'.

For me the issue was not creating a valid combination of source and switch elements to target this source. Here is an example that will log to a file called tracelog.txt. For the following code:

TraceSource source = new TraceSource("sourceName");
source.TraceEvent(TraceEventType.Verbose, 1, "Trace message");

I successfully managed to log with the following diagnostics configuration:

<system.diagnostics>
    <sources>
        <source name="sourceName" switchName="switchName">
            <listeners>
                <add
                    name="textWriterTraceListener"
                    type="System.Diagnostics.TextWriterTraceListener"
                    initializeData="tracelog.txt" />
            </listeners>
        </source>
    </sources>

    <switches>
        <add name="switchName" value="Verbose" />
    </switches>
</system.diagnostics>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shaun
  • 1,293
  • 16
  • 19
  • 1
    Code snippet does not work. – usefulBee Apr 13 '16 at 16:19
  • Apologies @usefulBee, it was not meant to be a code snippet, it was automatically marked up as code. This is configuration code that would be part of your App.config or Web.config, I hope that helps. – Shaun Apr 15 '16 at 10:06
3

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 configuration file and how to write the code, but it is pretty old (2002).

There's another article on CodeProject: A Treatise on Using Debug and Trace classes, including Exception Handling, but it's the same age.

CodeGuru has another article on custom TraceListeners: Implementing a Custom TraceListener

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Glenn Slaven
  • 33,720
  • 26
  • 113
  • 165