How can I output simple debugging messages

本文介绍如何在C#中使用Debug类输出简单的调试信息,包括如何在非调试模式下进行输出,以及如何利用Write*方法和条件输出功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 the OutputDebugString 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 your Debug.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, and WriteLineIf 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 by Write 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, the Debug.Write* calls are completely eliminated. Also, code generation can be controlled by defining the DEBUG 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值