36

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.

Zurechtweiser
  • 1,165
  • 2
  • 16
  • 29

11 Answers11

90

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
Guillermo Hernandez
  • 1,114
  • 1
  • 7
  • 6
33

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

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • I replaced Console.WriteLine("the message"); with MessageBox.Show("This is a message"); and that worked, so debug mode is on. – Zurechtweiser Feb 21 '12 at 11:15
  • Mono C# compiler’s `--debug` option (`mcs --debug`) only generates debug symbols in the binary, but it does not set `DEBUG`, nor `TRACE`. Use `mcs --debug -d:DEBUG -d:TRACE Program.cs` for `Debug.WriteLine()` and related to work. – Palec Mar 29 '17 at 17:44
  • I had to Clean Solution and Clean Project, and Build Solution, Rebuild Project. Then it worked again. – Alfred Wallace Apr 19 '19 at 20:25
8

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);
Zensar
  • 817
  • 4
  • 8
  • This is correct, I had a in my config which was stopping this – Sam Jan 18 '17 at 03:50
  • I opted for `System.Console.Error` instead of `System.Console.Out`. Debug messages should not be inseparably mixed with actual output; output redirection allows me to separate them in my setup. – Palec Mar 29 '17 at 17:37
  • [`ConsoleTraceListener`](https://msdn.microsoft.com/en-us/library/system.diagnostics.consoletracelistener.aspx) is a minimalistic subclass of `TextWriterTraceListener` that just provides constructors for convenient initialization to standard stream writers: `new ConsoleTraceListener()` uses `Console.Out`, `new ConsoleTraceListener(true)` uses `Console.Error`. [See its source.](https://referencesource.microsoft.com/#System/compmod/system/diagnostics/ConsoleTraceListener.cs) BTW, later `Console.SetOut()` and `Console.SetError()` calls do not influence them. – Palec Mar 29 '17 at 18:52
4

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

Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
  • 2
    Correct. You have to have a debugger "attached" – Matt Wilko Aug 11 '15 at 15:06
  • @MattWilko Good point. I've grown a habit of checking `Debugger.IsAttached` - https://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.isattached(v=vs.110).aspx if I need to do something special for debugging – Morten Jensen Jul 24 '18 at 10:47
2

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

Nissa
  • 4,636
  • 8
  • 29
  • 37
Serge V.
  • 3,377
  • 3
  • 20
  • 28
2

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.

Sofian Hnaide
  • 2,284
  • 16
  • 13
1

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.

Mark Ball
  • 366
  • 2
  • 10
1

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.

Community
  • 1
  • 1
thebenman
  • 1,621
  • 14
  • 35
1

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!]

LuminoDiode
  • 11
  • 1
  • 2
  • The option you refer to controls the level of output detail when building the program. The question was however related to the output when running the program. – Klaus Gütter Jun 14 '20 at 05:56
1

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.

0

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

kaarthick raman
  • 793
  • 2
  • 13
  • 41