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
):