Question

I followed this Create a Logger using the Trace Listener in C# article with the belief that only Trace.WriteLine() statements will be written in the application.log file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="application.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

But to my dismay, all of my Debug.WriteLine() that used to go to the Output tab disappeared from that tab and instead they all now go to the trace file application.log.

Is there a way to direct only Trace.WriteLine() statements to that application.log?

If not, what is the simplest way to WriteLine() to a file of my own only what I choose to log to that file, not my entire debug session?

Was it helpful?

Solution

Not sure what you've expected when <remove name="Default" /> but removing that line from config will get you back output in the Output tab. You can see that DefaultTraceListener uses OutputDebugString method that allows VS to show logs in Output tab.

Traces send to all listeners and listener itself can do filtering.

If you want to do some more categorization of traces - see other classes in System.Diagnostic namespace like TraceSource with samples in How to - Trace Source:

private static TraceSource mySource = new TraceSource("TraceSourceApp");
...
mySource.TraceEvent(TraceEventType.Error, 1, "Error message.");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top