简体   繁体   中英

How should I use debug.print in C#

In VB, we can use debug.print to print the text in a console, but how do we do the similar thing in C#? The objective is to test the written class.

Use Debug.WriteLine to write debug output.

System.Diagnostics.Debug.WriteLine("My text");
Debug.WriteLine("My text"); //assuming you have using System.Diagnostics

The output will be displayed in your IDE. Assuming Visual Studio, it will write to the Output window when you run the project. If you don't see the Output Window try View -> Output from the menu bar. When you see the Output window, make sure "Show Output From Debug " is selected.

You're looking for System.Diagnostics.Debug and/or System.Diagnostics.Trace . Pay attention to the difference (normally) between debug and release configuration.

Furthermore you might want to check out a logging framework like nlog or log4net.

SolutionsCS - that will write to the Console, not the Debug Console. If you then run the code in a non-debug environment it will still print out, which isn't really what you want for debugging messages.

Debug messages appear in the Output window if you select "Show Output From Debug".

If you want to see your debug messages without all the other cruft go to Tools->Options->Debugging->General and check Redirect all Output Window text to the Intermediate Window and, if the Intermediate Window is closed, follow xPert_Umer's instructions.

You can use Debug.WriteLine or Console.WriteLine .

Or write appropriate unit tests.

You can use System.Diagnostics.Debug.WriteLine to write text to the debug output. When you compile in release mode, these calls will be removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM