Zurechtweiser
Zurechtweiser

Reputation: 1216

Debug.WriteLine shows nothing

When using

using System.Diagnostics;

and

Debug.WriteLine("Test");

having run the application, no "Test" can be seen in output. But if I use the msgbox function instead, the msgbox pops up, so the line is reached.

Am I looking in the wrong window or what do I have to change?

I am using VC# Express.

Upvotes: 39

Views: 49772

Answers (11)

MostHost LA
MostHost LA

Reputation: 121

After pulling the project off a repo the settings in the launch button were different than they ought to be. It was set to publish. If you ended up here and the very first answer doesn't work, check it.

Project launch settings

Also using Trace instead of Debug seems to work when published is mistakenly selected.

Upvotes: 1

kaarthick raman
kaarthick raman

Reputation: 839

This issue occurred to me in a different way while deploying code to remote servers.

I already had a bunch of pre-configured build configurations in Configurations Manager. I always chose the staging environment settings while publishing. Since i stayed in debug mode in my local machine ,It never occured to me that the preconfigured build configuration also has a mode.

So do check for the config under settings- DEBUG or RELEASE and pick DEBUG

enter image description here

Also make sure you copy your .pdb files along with respective dlls.

Hope this helps someone

Upvotes: 0

LuminoDiode
LuminoDiode

Reputation: 11

What helped to me is selecting "Tools => Options => Projects and Solutions => Build and Run => MSBuild project build output verbosity" to "Diagnostics". After that it just started to show debug ouput in Debug block without any listeners adding. [You need at least 10 reputation to post images. Big thanks to SO for this!]

Upvotes: 1

thebenman
thebenman

Reputation: 1621

In case any of the others answer do not work. Try to place a break point in Debug.WriteLine line and see if it is being hit.

In case it is not being hit the reason is because an old version of the code is being executed.

To fix this, first check this

Source

Go to Tools-Options

Under Projects and solution -> Build and Run select "Always build" under "On Run, when projects are out of date"

enter image description here

And Check if the Temporary files are cleaned out. Check this SO Question

This worked for me.

If even this fails try rebooting VS. Works most of the time.

Upvotes: 1

Mark Ball
Mark Ball

Reputation: 376

For anyone googling: Whilst there's various answers that point to removal of the listeners in the config file also watch out for

<remove name="OPTIONSVerbHandler" />

in the section

<handlers>

As this also suppresses debug output.

Upvotes: 1

Serge V.
Serge V.

Reputation: 3613

The answer is easy. It might be that you press ctrl+F5 which means Starting Without Debugging. Simply press F5 to Start Debugging mode.

enter image description here

Upvotes: 2

Guillermo Hernandez
Guillermo Hernandez

Reputation: 1174

On Menu > tools > options > debugging > General:

  • Ensure "Redirect all output window text to the immediate window" is NOT checked

On Project Properties > Build:

  • Configuration: Debug
  • "Define DEBUG constant" is checked
  • "Define TRACE constant" is checked

On the Output window:

  • Show output from: Debug
  • Right-click in the output window and ensure "Program output" is checked

Upvotes: 96

Morten Jensen
Morten Jensen

Reputation: 5936

I'm not sure anyone has mentioned this reason, but if I compile in Debug-mode and then just run the program (Ctrl + F5) instead of choosing Start Debugging (F5), I won't see the Debug.WriteLine either.

So it isn't enough to simply compile in Debug mode, you also have to actively Debug the program and not just run it :)

Upvotes: 4

JaredPar
JaredPar

Reputation: 754565

There are two likely causes for this behavior

  • The application is being compiled in Release mode and the Debug.WriteLine call is not in the final program
  • There is no trace listener in the program and hence nothnig to output the message

The easiest way to diagnose this is to change the code to

#if DEBUG
Console.WriteLine("the message");
#endif

If it prints then you have an issue with the trace listeners, else you're compiling in Release

Upvotes: 36

Sofian Hnaide
Sofian Hnaide

Reputation: 2364

Debug.WriteLine("Test"); should be showing in the output window when you are in the debug mode. If you want to debug an application running (Release mode) you can use Trace and that would show in Windows events.

Upvotes: 2

Zensar
Zensar

Reputation: 817

I believe "Debug.WriteLine()" writes to the Listeners collection. From there you can determine where the debug information will be written. By default "Output" should be where it appears, but if you are having trouble viewing the information then create a different listener to grab the debug info.

Here is the MSDN example:

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

Upvotes: 8

Related Questions