167

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • as mentioned at some comment in another reply, can use Microsoft's (SysInternals) DebugView: http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx – George Birbilis Sep 26 '14 at 15:10

8 Answers8

125

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".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
boardernin
  • 1,251
  • 2
  • 7
  • 2
  • 5
    I would like to confirm that (Ctrl + Alt + O) will bring up the output window while debugging in Visual Studio 2012 – Ishikawa Aug 26 '12 at 18:54
82

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

Tormod Fjeldskår
  • 5,952
  • 1
  • 29
  • 47
  • 6
    It was useful (didn't expect that) for me to find out that Debug and Trace objects use the same trace listeners, therefore, both Debug.Write and Trace.Write output to the same place(s), it is just that depending on conditions they might not get executed – hello_earth Feb 14 '11 at 16:54
  • 1
    could you elaborate on what *conditions* they are? exactly which situation a debug.Write will not work the same as a trace.write? – Pacerier Jul 29 '11 at 16:41
  • 2
    this doesnt work for me, i have debug and trace flags set in vs project properties, and am using Debug.WriteLine, the line executes, but nothing appears in the output window, anyone got any other advise? – f1wade Dec 16 '14 at 13:12
  • To expand on @hello_earth comment, see https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.write?view=net-5.0 – Kevin S. Miller Feb 16 '21 at 19:34
50

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.

jason
  • 236,483
  • 35
  • 423
  • 525
  • 2
    Apparently DebugView will capture both the .NET Debug.Write() and Win32 OutputDebugString(): http://technet.microsoft.com/en-us/sysinternals/bb896647 – dlchambers Dec 15 '11 at 17:29
  • @dlchambers: I don't think that's correct. That page claims display of `OutputDebugString()` and (kernel) `DbgPrint().` – Mike C Jan 22 '15 at 00:50
  • 1
    @dlchambers: I withdraw the comment; I discovered that DebugView can capture `Debug.Write()` *if* its Capture settings include "Global Win32" -- which requires running it in Admin mode. – Mike C Jan 22 '15 at 01:34
13

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

Pat
  • 5,263
  • 1
  • 36
  • 53
7

The Diagnostics messages are displayed in the Output Window.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
5

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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kykbr
  • 266
  • 1
  • 5
  • 13
2

The solution for my case is:

  1. Right click the output window;
  2. Check the 'Program Output'
Ning Zhu
  • 59
  • 1
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthis Kohli
  • 1,877
  • 1
  • 21
  • 23