Visual C++ 中的 Debug 类

更新:2007 年 11 月

在 Visual C++ 应用程序中使用 Debug 时,行为在调试版本和发布版本之间不发生更改。

备注

Trace 的行为与 Debug 类的行为相同,但是依赖于正被定义的符号 TRACE。这意味着必须 #ifdef 任何与 Trace 相关的代码,以防止在发布版本中出现调试行为。

示例

说明

无论您是用 /DDEBUG 还是用 /DTRACE 编译,下面的示例总是执行输出语句。

代码

// mcpp_debug_class.cpp
// compile with: /clr
#using <system.dll>
using namespace System::Diagnostics;
using namespace System;

int main() {
   Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
   Trace::AutoFlush = true;
   Trace::Indent();
   Trace::WriteLine( "Entering Main" );
   Console::WriteLine( "Hello World." );
   Trace::WriteLine( "Exiting Main" );
   Trace::Unindent();

   Debug::WriteLine("test");
}

输出

    Entering Main
Hello World.
    Exiting Main
test

示例

说明

若要获得预期的行为(即发布版本不打印“test”输出),必须使用 #ifdef 和 #endif 指令。上述代码示例经过如下修改以说明此修复:

代码

// mcpp_debug_class2.cpp
// compile with: /clr
#using <system.dll>
using namespace System::Diagnostics;
using namespace System;

int main() {
   Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
   Trace::AutoFlush = true;
   Trace::Indent();

#ifdef TRACE   // checks for a debug build
   Trace::WriteLine( "Entering Main" );
   Console::WriteLine( "Hello World." );
   Trace::WriteLine( "Exiting Main" );
#endif
   Trace::Unindent();

#ifdef DEBUG   // checks for a debug build
   Debug::WriteLine("test");
#endif   //ends the conditional block
}

请参见

其他资源

.NET 编程指南