<p>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:</p>
<ul>
<li>App.config / Web.config stuff to add for registering TraceListeners</li>
<li>how to set it up in the calling application</li>
</ul>
<p>Do you know the über tutorial that we should link to?</p>
<hr/>
<p>Glenn Slaven pointed me in the right direction. Add this to your App.config/Web.config inside <code>&lt;configuration/&gt;</code>:</p>
<pre><code>&lt;system.diagnostics&gt;
    &lt;trace autoflush=&quot;true&quot;&gt;
      &lt;listeners&gt;
        &lt;add type=&quot;System.Diagnostics.TextWriterTraceListener&quot; name=&quot;TextWriter&quot;
             initializeData=&quot;trace.log&quot; /&gt;
      &lt;/listeners&gt;
    &lt;/trace&gt;
&lt;/system.diagnostics&gt;
</code></pre>
<p>This will add a <code>TextWriterTraceListener</code> that will catch everything you send to with <code>Trace.WriteLine</code>, etc.</p>
<p>@DanEsparza pointed out that you should use <code>Trace.TraceInformation</code>, <code>Trace.TraceWarning</code> and <code>Trace.TraceError</code> instead of <code>Trace.WriteLine</code>, as they allow you to format messages the same way as <code>string.Format</code>.</p>
<p><strong>Tip:</strong> If you don't add any listeners, then you can still see the trace output with the Sysinternals program <a href="https://learn.microsoft.com/en-us/sysinternals/downloads/debugview" rel="nofollow noreferrer">DebugView</a> (<code>Dbgview.exe</code>):</p>

[{'answer_id': 27659, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 2, 'selected': False, 'text': '<p>DotNetCoders has a starter article on it: <a href="http://www.dotnetcoders.com/web/Articles/ShowArticle.aspx?article=50" rel="nofollow noreferrer">http://www.dotnetcoders.com/web/Articles/ShowArticle.aspx?article=50</a>. 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).</p>\n<p>There\'s another article on CodeProject: <em><a href="http://www.codeproject.com/KB/trace/debugtreatise.aspx" rel="nofollow noreferrer">A Treatise on Using Debug and Trace classes, including Exception Handling</a></em>, but it\'s the same age.</p>\n<p>CodeGuru has another article on custom TraceListeners: <em><a href="https://www.codeguru.com/vb/gen/vb_misc/debuggingandtracing/article.php/c5611/Implementing-a-Custom-TraceListener.htm" rel="nofollow noreferrer">Implementing a Custom TraceListener</a></em></p>\n'}, {'answer_id': 35034967, 'author': 'Shaun', 'author_id': 276874, 'author_profile': 'https://Stackoverflow.com/users/276874', 'pm_score': 3, 'selected': False, 'text': '<p>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 <code>TraceSource.TraceEvent(TraceEventType,\u2002Int32,\u2002String)</code> method where the <code>TraceSource</code> object was initialised with a string making it a \'named source\'.</p>\n<p>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 <code>tracelog.txt</code>.  For the following code:</p>\n<pre><code>TraceSource source = new TraceSource(&quot;sourceName&quot;);\nsource.TraceEvent(TraceEventType.Verbose, 1, &quot;Trace message&quot;);\n</code></pre>\n<p>I successfully managed to log with the following diagnostics configuration:</p>\n<pre class="lang-html prettyprint-override"><code>&lt;system.diagnostics&gt;\n    &lt;sources&gt;\n        &lt;source name=&quot;sourceName&quot; switchName=&quot;switchName&quot;&gt;\n            &lt;listeners&gt;\n                &lt;add\n                    name=&quot;textWriterTraceListener&quot;\n                    type=&quot;System.Diagnostics.TextWriterTraceListener&quot;\n                    initializeData=&quot;tracelog.txt&quot; /&gt;\n            &lt;/listeners&gt;\n        &lt;/source&gt;\n    &lt;/sources&gt;\n\n    &lt;switches&gt;\n        &lt;add name=&quot;switchName&quot; value=&quot;Verbose&quot; /&gt;\n    &lt;/switches&gt;\n&lt;/system.diagnostics&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/27610', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2260/']