title: .NET tracing not working with Diagnostics.TraceSource, only Diagnostics.Trace
body: <p>I’m trying to set up .NET tracing.  I’m able to get basic tracing to work via System.Diagnostics.Trace, but for complicated reasons I have to activate tracing via System.Diagnostics.TraceSource objects (the new way of doing it, since .NET 2.0) rather than using System.Diagnostics.Trace.  I've tried everything but it just doesn't want to work using TraceSource.  I am performing the tracing in an ASP.NET code-behind (aspx.cs)</p>
  
  <p>Here are some related URLs:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/ty48b824.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ty48b824.aspx</a><br>
  <a href="http://msdn.microsoft.com/en-us/library/64yxa344.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/64yxa344.aspx</a><br>
  <a href="http://msdn.microsoft.com/en-us/library/sk36c28t.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/sk36c28t.aspx</a><br>
  <a href="http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396431.aspx" rel="noreferrer">http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396431.aspx</a><br>
  <a href="http://msdn.microsoft.com/en-us/library/b0ectfxd%28v=VS.100%29.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/b0ectfxd%28v=VS.100%29.aspx</a>  </p>
  
  <p>Currently, based on what is in web.config, it should be tracing both to a file and to the page, from this code:</p>
  
  <pre><code>TraceSource ts = new TraceSource("mysource", SourceLevels.All);
  Trace.Write("Trace (old way)"); // this one works
  ts.TraceInformation("Trace (new way)"); // this one doesn't work
  ts.Flush();
  ts.Close();
  </code></pre>
  
  <p>Here’s the web.config sections that are relevant:</p>
  
  <pre><code> &lt;system.diagnostics&gt;
         &lt;trace autoflush="false"&gt;
              &lt;listeners&gt; &lt;!-- these listeners activate the "old way" of tracing. --&gt;
                   &lt;add       name="pagelistener" /&gt;
                   &lt;add       name="filelistener" /&gt;
              &lt;/listeners&gt;
         &lt;/trace&gt;
  
         &lt;sources&gt;
              &lt;source name="mysource" switchName="myswitch"&gt;
                   &lt;listeners&gt;  &lt;!-- these listeners activate the "new way" --&gt;
                         &lt;add name="pagelistener" /&gt;
                         &lt;add name="filelistener" /&gt;
                   &lt;/listeners&gt;
              &lt;/source&gt;
         &lt;/sources&gt;
  
  
         &lt;sharedListeners&gt; 
              &lt;!-- these are the actual trace listeners --&gt;
              &lt;add
                    name="filelistener"
                   type="System.Diagnostics.TextWriterTraceListener"
                   initializeData="loplog.txt"
                   /&gt;
              &lt;add
                   name="pagelistener"
                   traceOutputOptions="none"
                   type="System.Web.WebPageTraceListener, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                   /&gt;
         &lt;/sharedListeners&gt;
  
         &lt;switches&gt;
              &lt;!-- the sources above use this verbose switch --&gt;
              &lt;add name="myswitch" value="Verbose"/&gt;
         &lt;/switches&gt;
  
   &lt;/system.diagnostics&gt;
   &lt;system.codedom&gt;
         &lt;!-- this compilers section should not be needed because I added
                   #define TRACE to the .aspx.cs file, however I put this in
                   since it's still not working. --&gt;
  
         &lt;compilers&gt;
              &lt;compiler
                              language="c#;cs;csharp"
                              extension=".cs"
                              compilerOptions="/d:TRACE"
                              type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                              warningLevel="1"
                              /&gt;
         &lt;/compilers&gt;
   &lt;/system.codedom&gt;
  
   &lt;system.web&gt;
         &lt;!-- this trace tag should be redundant because I added trace="true" to the aspx file,
                   but I put it in here anyway because this wasn't working. --&gt;
         &lt;trace writeToDiagnosticsTrace="true" enabled="true" pageOutput="true" requestLimit="50" localOnly="true"/&gt;
  </code></pre>