37

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.

3
  • 1
    of course you are running your app inside the vs ide? right?
    – Steve
    Commented Feb 20, 2012 at 23:15
  • 1
    @Henk Holterman Output, show output from "debug" on the bottommost. Commented Feb 20, 2012 at 23:19
  • 1
    @Steve well I press the green triangle that says f5 start debugging inside the ide Commented Feb 20, 2012 at 23:19

11 Answers 11

93

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
1
  • 1
    Define DEBUG constant is the solution for me. thanks
    – beginners
    Commented Jul 7, 2021 at 1:42
35

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

4
  • I replaced Console.WriteLine("the message"); with MessageBox.Show("This is a message"); and that worked, so debug mode is on. Commented Feb 21, 2012 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
    Commented Mar 29, 2017 at 17:44
  • I had to Clean Solution and Clean Project, and Build Solution, Rebuild Project. Then it worked again. Commented Apr 19, 2019 at 20:25
  • Didn't work when compiling in Test mode either. I switched to Debug and it worked. Thanks for pointing me in the right direction.
    – bowend01
    Commented Dec 4, 2023 at 16:40
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);
3
  • This is correct, I had a <clear /> in my config which was stopping this
    – Sam
    Commented Jan 18, 2017 at 3: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
    Commented Mar 29, 2017 at 17:37
  • ConsoleTraceListener 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. BTW, later Console.SetOut() and Console.SetError() calls do not influence them.
    – Palec
    Commented Mar 29, 2017 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 :)

2
  • 2
    Correct. You have to have a debugger "attached"
    – Matt Wilko
    Commented Aug 11, 2015 at 15:06
  • @MattWilko Good point. I've grown a habit of checking Debugger.IsAttached - msdn.microsoft.com/en-us/library/… if I need to do something special for debugging Commented Jul 24, 2018 at 10:47
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.

1
  • 1
    that's where i am looking. Output, show output from "debug" on the bottommost. Commented Feb 20, 2012 at 23:19
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

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.

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.

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

1
  • 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. Commented Jun 14, 2020 at 5: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.