Why is my Debug.Write broken?

2020-03-05 20:22发布

问题:

Can somebody explain why my Debug.Write stops working for no apparent reason - no output is visible? This has happened to me many times in Visual Studio 2008 (never on 2005), on both Windows XP, Windows 2003 Server and Windows 7.

It is per project and at the same time (on Windows 2003 server), I have projects where Debug.Write works and does not work.

Currently, on Windows 7 I have no debug output at all. I create a new windows forms project, put Debug.Write("bla") in Form_Load run it, and nothing happens in Output window. Same behaviour is in both vb.net and c# projects.

UPDATE: I just found out that my debug output is being shown, but in my Immediate window, instead of output. Why???

UPDATE: In Visual Studio options, there is a setting under Debugging - General: Redirect all Output window text to Immediate window.

Why was this checked by default, I have no idea...

回答1:

Tools > Options > Debugging, General has a setting that redirects all output to the immediate window.



回答2:

If your definately running in Debug Mode then I would suggest a quick noddy project with a simple Debug.Print in a form load. If you can get it to work in a noddy project then it might be to do with the extra References you have including in your original project.

Is it appearing in any of the windows?


EDIT: Found this here

a) Go to menu Tools, then Options. Here make sure you have the "Show all settings" on the lower left side of the options screen selected.

b ) Navigate to: Debugging -> General

c) Make sure you have the option "Redirect all Output Window text to the Immediate Window"



回答3:

Are you building a release build by mistake? A release build will not compile in the Debug.Write() calls.

From the MSDN docs (http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.aspx):

The ConditionalAttribute attribute is applied to the methods of Debug. Compilers that support ConditionalAttribute ignore calls to these methods unless "DEBUG" is defined as a conditional compilation symbol. Refer to a compiler's documentation to determine whether ConditionalAttribute is supported and the syntax for defining a conditional compilation symbol.

To define the "DEBUG" conditional compilation symbol in C# and J#, add the /d:DEBUG option to the compiler command line when you compile your code or add #define DEBUG to the top of your file. In Visual Basic, add the /d:DEBUG=True option to the compiler command line or add #Const DEBUG=True to the file.

Note also that the Debug.Write() method sends its output to the TraceListeners collection, which can have listeners added or removed at runtime or by a configuration file - make sure that any configuration file isn't changing where the debug output should be going.



回答4:

In the Output window are you sure that you have "Debug" selected from the "Show output from:" drop-down? Just a thought!