Archive for January, 2009

Logging trace output

I was recently testing out a third party tool and wanted to log some of the trace information, here is how you do it:

* Note that in this example, the trace is simply being sent to the console, but you could write it to a file / database …


    1 // Place the code below at the beginning of your execution context

    2 StringBuilder builder = new StringBuilder();

    3 TextWriter writer = new StringWriter(builder);

    4 TextWriterTraceListener listner = new TextWriterTraceListener(writer);

    5 Trace.Listeners.Add(listner);

    6 

    7 // Then do something here

    8 

    9 

   10 // Place the code below at the end of your execution context to log to file / database / console …

   11 Console.Write(builder.ToString());