previous_developer
previous_developer

Reputation: 11008

Writing to output window of Visual Studio

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.

Upvotes: 774

Views: 961963

Answers (13)

Vurdalakov
Vurdalakov

Reputation: 109

Yet another way to write traces to the "Output" window of debugger is to use System.Diagnostics.Debugger.Log method:

public static void WriteToDebugger(String message)
{
    Debugger.Log(0, null, message);
    Debugger.Log(0, null, Environment.NewLine);
}

Upvotes: 0

dansasu11
dansasu11

Reputation: 913

You may be looking for

MessageBox.Show("A message")

or

Debug.WriteLine("A message")

Upvotes: 18

majjam
majjam

Reputation: 1326

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

Upvotes: 0

Chris McCowan
Chris McCowan

Reputation: 487

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

Upvotes: 9

mikro
mikro

Reputation: 525

Debug.Print("text here")

or

Console.WriteLine("text here")

Upvotes: 3

Micah Armantrout
Micah Armantrout

Reputation: 6981

Debug.WriteLine

is what you're looking for.

If not, try doing this:

Menu ToolsOptionsDebugging → uncheck Send Output to Immediate.

Upvotes: 88

Mona Jalal
Mona Jalal

Reputation: 38255

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)

Upvotes: 0

djabraham
djabraham

Reputation: 776

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"

Upvotes: 2

jpmc26
jpmc26

Reputation: 29954

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.

Upvotes: 5

Paul Gorbas
Paul Gorbas

Reputation: 1792

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.

Upvotes: 14

Zac
Zac

Reputation: 4705

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.

Upvotes: 33

Bhargav
Bhargav

Reputation: 10209

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:

Upvotes: 936

veight
veight

Reputation: 2177

This will write to the debug output window:

using System.Diagnostics;

Debug.WriteLine("Send to debug output.");

Upvotes: 198

Related Questions