2

New to c# and .net, working in visual studio 2012. I want to have the ability to display values to an output window for debug purposes. I am very familiar with the watch windows, but that does not meet my current needs.

My most recent effort I took the exact sample from the msdn website.

   Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
   Debug.AutoFlush = true;
   Debug.Indent();
   Debug.WriteLine("Entering Main");
   Console.WriteLine("Hello World.");
   Debug.WriteLine("Exiting Main"); 
   Debug.Unindent();

This example can be found at http://msdn.microsoft.com/en-us/library/system.diagnostics.debug%28v=vs.71%29.aspx

I added a breakpoint on the very next line after the above code. When I hit the breakpoint I have no errors displaying and my output window has none of the anticipated content. I do have the using System.Diagnostics set.

Tom
  • 1,971
  • 3
  • 22
  • 32

1 Answers1

3

As described in MSDN, System.Diagnostics.Debug writes their messages into all subscribed TraceListeners (http://msdn.microsoft.com/en-us/library/system.diagnostics.tracelistener.aspx). Note that you should #define TRACE or add /d:TRACE when you compile your program. You can setup your listeners in configuration file like this.

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>
Viacheslav Kovalev
  • 1,745
  • 12
  • 17
  • NOTE: The debug and trace do not go into an output window as anticipated, but write to the file set in the configuration. In the example here that file name is TextWriterOutput.log. – Tom Mar 04 '13 at 13:53