Where does System.Diagnostics.Debug.Write output appear?

C#Debugging

C# Problem Overview


The following C# program (built with csc hello.cs) prints just Hello via Console! on the console and Hello via OutputDebugString in the DebugView window. However, I cannot see either of the System.Diagnostics.* calls. Why is that?

using System;
using System.Runtime.InteropServices;
class Hello {
    [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
    public static extern void OutputDebugString(string message);

    static void Main() {
        Console.Write( "Hello via Console!" );
        System.Diagnostics.Debug.Write( "Hello via Debug!" );
        System.Diagnostics.Trace.Write( "Hello via Trace!" );
        OutputDebugString( "Hello via OutputDebugString" );
    }
}

Is there maybe some special command-line switches required for csc?

I'm not using Visual Studio for any of my development, this is pure commandline stuff.

C# Solutions


Solution 1 - C#

While debugging System.Diagnostics.Debug.WriteLine will display in the output window (Ctrl+Alt+O), you can also add a TraceListener to the Debug.Listeners collection to specify Debug.WriteLine calls to output in other locations.

Note: Debug.WriteLine calls may not display in the output window if you have the Visual Studio option "Redirect all Output Window text to the Immediate Window" checked under the menu ToolsOptionsDebuggingGeneral. To display "ToolsOptionsDebugging", check the box next to "ToolsOptionsShow All Settings".

Solution 2 - C#

As others have pointed out, listeners have to be registered in order to read these streams. Also note that Debug.Write will only function if the DEBUG build flag is set, while Trace.Write will only function if the TRACE build flag is set.

Setting the DEBUG and/or TRACE flags is easily done in the project properties in Visual Studio or by supplying the following arguments to csc.exe

> /define:DEBUG;TRACE

Solution 3 - C#

You need to add a TraceListener to see them appear on the Console.

TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);

They also appear in the Visual Studio Output window when in Debug mode.

Solution 4 - C#

While you are debugging in Visual Studio, display the "Output" window (View->Output). It will show there.

Solution 5 - C#

The Diagnostics messages are displayed in the Output Window.

Solution 6 - C#

When I write debug.write("") in the code, it outputs in the "Immediate window", not "Output window".

You can try it. For displaying the "Immediate" window (DebugWindowImmediate).

Solution 7 - C#

The solution for my case is:

  1. Right click the output window;
  2. Check the 'Program Output'

Solution 8 - C#

For VB.NET the following applies. You have to select "Debug" AND make sure that you "Start Debugging". This can be reached by pressing F5.

Also the Console.WriteLine will only display messages when building as "Release" in your Output window.

As mentioned before, open the Output window with ViewOutput AND make sure to select either "Build" if you want to see Console.WriteLine messages or "Debug" if you want to see Debug.WriteLine or Trace.WriteLine messages.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrerich RaabeView Question on Stackoverflow
Solution 1 - C#boarderninView Answer on Stackoverflow
Solution 2 - C#Tormod FjeldskårView Answer on Stackoverflow
Solution 3 - C#jasonView Answer on Stackoverflow
Solution 4 - C#PatView Answer on Stackoverflow
Solution 5 - C#Andreas GrechView Answer on Stackoverflow
Solution 6 - C#kykbrView Answer on Stackoverflow
Solution 7 - C#Ning ZhuView Answer on Stackoverflow
Solution 8 - C#Matthis KohliView Answer on Stackoverflow