657

I am trying to write a message to the output window for debugging purposes. I searched for a function like Java's system.out.println(""). I tried Debug.Write, Console.Write, and Trace.Write. It does not give an error, but it does not print anything either.

"Define DEBUG constant" and "Define TRACE constant" options are checked.

Menu ToolsOptionsDebugging"Redirect all Output Window text to the Immediate Window" option is not checked.

Configuration: Active (Debug)

Note: I created a project with the wizard as "Windows Forms Application" if relevant. I have no idea where to look.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
previous_developer
  • 9,317
  • 6
  • 37
  • 57
  • 10
    Since this is an older post, I'll add this as a comment for those who stumble across the question. Instead of actually changing code, you can also use special breakpoints called tracepoints. See [MSDN documentation](http://msdn.microsoft.com/en-us/library/vstudio/232dxah7(v=vs.100).aspx) – Wonko the Sane Mar 22 '13 at 15:15
  • 13
    Just a reminder that Debug.WriteLine() will only work when running in Debug. That means running it with F5 and not CTRL-F5. This is easy to miss. – kirk.burleson Jul 15 '13 at 14:38
  • 3
    That's true, but a handy tool is [DebugView](http://technet.microsoft.com/en-gb/sysinternals/bb896647.aspx) which shows all output to the debug stream when running outside of the debugger – the_mandrill Aug 23 '13 at 13:39
  • 2
    If you are trying to write output from a unit test running under the Visual Studio test framework the rules are a little different, see [this answer](http://stackoverflow.com/questions/11209639/can-i-write-into-console-in-a-unit-test-if-yes-why-the-console-window-is-not-o) for details. – yoyo Mar 08 '16 at 19:54
  • Just to add on the comment @kirk.burleson made; if you use `Debug.Write` in a library, and you build the library in release mode (which is often the case with nuget packages), then it won't log even if you run your tests/application in debug mode. I would suggest `Trace.Write` as an alternative – Rob Jan 21 '19 at 13:19

12 Answers12

810

Add the System.Diagnostics namespace, and then you can use Debug.WriteLine() to quickly print a message to the output window of the IDE. For more details, please refer to these:

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Bhargav Bhat
  • 8,939
  • 1
  • 17
  • 29
167

This will write to the debug output window:

using System.Diagnostics;

Debug.WriteLine("Send to debug output.");
veight
  • 1,867
  • 1
  • 11
  • 5
  • 7
    For me this "outputs" to the Immediate window – nuander Mar 10 '15 at 22:28
  • I tried this on a set of unit tests. I debugged the selected test, but there was nothing of what I tried to print out in the debug window. – Frank H. Apr 22 '15 at 16:13
  • This does output to the Debug window, however, only after I close the web page. I would like an immediate display when I execute the code. Wait a minute - I found that the output window is hidden while the code is executing. If I display the window, my output is there. – Joe Cotton Nov 10 '16 at 19:37
  • Can we keep a log of this message that is printed in debug window ? or does visual studio provides any way to access the logs of debug window..? – Anmol Rathod Sep 11 '19 at 09:09
60
Debug.WriteLine

is what you're looking for.

If not, try doing this:

Menu ToolsOptionsDebugging → uncheck Send Output to Immediate.

TylerH
  • 19,065
  • 49
  • 65
  • 86
Micah Armantrout
  • 6,072
  • 3
  • 33
  • 59
  • 1
    Possibly Debug.Write() is not inserting the carriage return, so messages are getting buffered up? – Guy Starbuck Feb 27 '12 at 14:44
  • 2
    I was not seeing the Debug.WriteLine("String") until after I did what Micah said. `Tools|Options|Debugging uncheck Send Output to Immediate` – Skindeep2366 Sep 25 '13 at 18:29
  • 4
    I think in the later versions the check box is "Redirect all Output Window text to Immediate Window" – Houdini Sutherland Feb 12 '15 at 21:05
  • 8
    in VS 2015 it's called: "Redirect all Output Window text to the Immediate Window", just in case, some had to look for 5 mins to find the appropriate setting :) - I was scrolling up and down to find an optiona starting with "Send..." :) – dba Jun 01 '16 at 08:49
26

For me, only the Trace namespace and not the Debug one worked:

System.Diagnostics.Trace.WriteLine("message");

I'm working in a C# project under Visual Studio 2010.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Zac
  • 4,076
  • 3
  • 33
  • 40
  • 2
    I'm using VS 2013 Professional and attempting to get output from a unit test using the Visual Studio unit test framework. It appears the test harness captures and redirects Debug.WriteLine but not Trace.WriteLine. And you won't see Trace output either unless you debug the tests (not just run them). – yoyo Mar 07 '16 at 23:27
15

You may be looking for

MessageBox.Show()

or

Debug.Writeline()
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
dansasu11
  • 797
  • 1
  • 7
  • 16
10

The call

System.Diagnostics.Debug.WriteLine("message");

fails when working with .NET Core (V 1.0 or 1.1).

We are supposed to create and use a logger from Microsoft.Extensions.Logging, but that log only appears in the dotnet.exe popup console window, not in Visual Studio's Output window.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Paul Gorbas
  • 1,415
  • 17
  • 15
  • So Debug.WriteLine(), Console.WriteLine() and Trace.WriteLine() are not outputting anything in Output for .Net Core? Any way to use some sort of simple logging without having to write a Logclass? Have searched in Tools > Options > Debugging and checked the "Show output from:" for possible solutions. – Wouter Vanherck Mar 08 '18 at 08:00
5

This requires a third party framework, namely Serilog, but I've nonetheless found it to be a very smooth experience with getting output to some place I can see it.

You first need to install Serilog's Trace sink. Once installed, you need to set up the logger like this:

Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Trace()
    .CreateLogger();

(You can set a different minimum level or set it to a config value or any of the normal Serilog functionality. You can also set the Trace logger to a specific level to override configs, or however you want to do this.)

Then you just log messages normally and they show up in your Output window:

Logger.Information("Did stuff!");

This doesn't seem like such a big deal, so let me explain some additional advantages. The biggest one for me was that I could simultaneously log to both the Output window and the console:

Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Trace()
    .WriteTo.Console(standardErrorFromLevel: LogEventLevel.Error)
    .CreateLogger();

This gave me great flexibility in terms of how I consumed output, without having to duplicate all my calls to Console.Write with Debug.Write. When writing the code, I could run my command line tool in Visual Studio without fear of losing my output when it exited. When I had deployed it and needed to debug something (and didn't have Visual Studio available), the console output was readily available for my consumption. The same messages can also be logged to a file (or any other kind of sink) when it's running as a scheduled task.

The bottom line is that using Serilog to do this made it really easy to dump messages to a multitude of destinations, ensuring I could always readily access the output regardless of how I ran it.

It also requires very minimal set up and code.

jpmc26
  • 23,237
  • 9
  • 76
  • 129
  • 1
    Serilog now also has a "debug" sink at https://github.com/serilog/serilog-sinks-debug that functions in a similar way, but has slightly nicer formatting :-) HTH! – Nicholas Blumhardt Oct 04 '17 at 22:22
  • Why not make a .net public(may be static) method and put there everything you need like Console.Write or Debug.Write etc and use it everywhere? – Saulius Feb 21 '19 at 10:34
  • @Saulius "The same messages can also be logged to a file (or any other kind of sink) when it's running as a scheduled task. The bottom line is that using Serilog to do this made it really easy to dump messages to a multitude of destinations, ensuring I could always readily access the output regardless of how I ran it." – jpmc26 Feb 21 '19 at 10:58
2

This is not an answer to the original question. But since I found this question when searching for a means of interactively dumping object data, I figured others may benefit from mentioning this very useful alternative.

I ultimately used the Command Window and entered the Debug.Print command, as shown below. This printed a memory object in a format that can be copied as text, which is all I really needed.

> Debug.Print <item>

  id: 1
  idt: null
  igad: 99
  igbd: 99
  gl_desc: "New #20"
  te_num: "1-001-001-020"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
djabraham
  • 766
  • 11
  • 20
  • Yep. Or when you want to get the JSON from a model to supply to a colleague `System.Diagnostics.Debug.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(response));` – rob_james Jan 15 '21 at 09:14
2
Debug.Print("text here")

or

Console.WriteLine("text here")
mikro
  • 430
  • 1
  • 5
  • 9
1

For debugging purposes, the System.Diagnostics.Debug.WriteLine() command will not be compiled into the release version of your code unless you have debug listeners. It writes to all trace listeners which includes the VS output window when running in Debug mode.

For a Console application. Console.WriteLine() would work but the output would still be generated in the release version of your binary.

Debug output should also appear in the normal output window when debugging tests; whereas, console.writeline output does not (but can be found in the test output window.)

Chris McCowan
  • 338
  • 3
  • 9
0

For anyone using NCrunch, Debug output is redirected to the NCrunch 'Trace Output' window.

majjam
  • 1,171
  • 2
  • 13
  • 27
-1

The following worked for me in Visual Studio 2015:

OutputDebugStringW(L"Write this to Output window in VS14.");

Read the documentation for OutputDebugStringW here.

enter image description here Note that this method only works if you are debugging your code (debug mode)

TylerH
  • 19,065
  • 49
  • 65
  • 86
Mona Jalal
  • 24,172
  • 49
  • 166
  • 311