title: How to log System.Diagnostics.Trace and System.Diagnostics.Debug messages to NLog file?
body: <p>I'm supporting a large, old application.  The code uses NLog as well as <code>Debug.WriteLine</code> and <code>Trace.WriteLine</code>.  I'm trying to redirect the Debug.WriteLine and the Trace.WriteLine to the Nlog file.  So that everything is in one place.  At the moment, some is in the Output window and some is in the log file.</p>
  <p><a href="http://nlog-project.org/2010/09/02/routing-system-diagnostics-trace-and-system-diagnostics-tracesource-logs-through-nlog.html" rel="nofollow noreferrer">I followed this article to use the NLogTraceListener</a></p>
  <p>That article has System.Net and System.Net.Sockets getting shunted to the log file.  I tried it and it works.  I see System.Net and System.Net.Sockets messages getting logged to the file.  <a href="https://stackoverflow.com/questions/13260672/automatically-log-system-diagnostics-trace-messages-to-an-nlog-target">This SO article</a> describes the same -- and it works.</p>
  <p>What doesn't work is logging my own messages from my own namespaces and classes to the log file.</p>
  <pre class="lang-cs prettyprint-override"><code>System.Diagnostics.Debug.WriteLine(&quot;This won't end up in the log file&quot;);
  System.Diagnostics.Trace.WriteLine(&quot;This won't end up in the log file&quot;);
  </code></pre>
  <p>I've tried changing the source names to namespaces from my app.  Didn't work. I cannot get my own WriteLines to log to the file.  They still appear in the Visual Studio output window.</p>
  <p>For instance:</p>
  <pre class="lang-xml prettyprint-override"><code>    &lt;system.diagnostics&gt;
          &lt;sources&gt;
              &lt;source name=&quot;MyAppNamespace&quot; switchValue=&quot;All&quot;&gt;
                  &lt;listeners&gt;
                      &lt;add name=&quot;nlog&quot; /&gt;
                  &lt;/listeners&gt;
              &lt;/source&gt;
          &lt;/sources&gt;
          &lt;sharedListeners&gt;
              &lt;add name=&quot;nlog&quot; type=&quot;NLog.NLogTraceListener, NLog&quot; /&gt;
          &lt;/sharedListeners&gt;
      &lt;/system.diagnostics&gt;
  </code></pre>
  <p>The explicit NLog calls are logging to the log file just fine:</p>
  <pre class="lang-cs prettyprint-override"><code>NLog.LogManager.GetCurrentClassLogger().Trace(&quot;This Works&quot;);
  </code></pre>