How can I output simple debugging messages

来源:互联网 发布:mac玫红色口红哪个最火 编辑:程序博客网 时间:2024/05/24 02:31

How can I output simple debugging messages?

Although Visual Studio sports many useful debugging features including line-by-line stepping through source code, there are times when outputting simple text strings with variable values for debugging is more efficient.

By using the Write* methods of the System.Diagnostics.Debug class, messages can be output in a way similar to theOutputDebugString function of the Win32 API. But, the charm of the Debug class is that when building an application using the default Visual Studio Release configuration, no source code lines are generated for yourDebug.Write* class. Therefore, no performance penalty is incurred by using the Debug class in release code.

To use the Debug class, follow this example:

using System.Diagnostics; Debug.Write ("Debugging string");

In addition to the Write method, WriteIf, WriteLine, andWriteLineIf may be called. Here is a brief example:

bool @flag1 = true;bool flag2  = false;Debug.WriteLineIf (@flag1 || flag2, "Conditional debug message!");

When debugging an application with the Visual Studio debugger, all messages emitted byWrite method calls show up in the Output window (View / Output menu command or Ctrl+W,O). However, when running an application outside the debugger—e.g., after starting it from Windows Explorer—, the messages can still be viewed using tools such as DebugView of Sysinternals.

Note: If the application is built using the default Release configuration, not even DebugView will display the messages; because, theDebug.Write* calls are completely eliminated. Also, code generation can be controlled by defining theDEBUG conditional directive.

Tip: The .NET debugging and tracing architecture allows redirecting debugging messages to various destinations,e.g. text files.

http://en.csharp-online.net/CSharp_FAQ%3A_How_can_I_output_simple_debugging_messages
原创粉丝点击