Debug 类 (C++/CLI)

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

备注

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

示例

ms235216.collapse_all(zh-cn,VS.110).gif说明

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

ms235216.collapse_all(zh-cn,VS.110).gif代码

// 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");
}

ms235216.collapse_all(zh-cn,VS.110).gifOutput

    Entering Main
Hello World.
    Exiting Main
test

示例

ms235216.collapse_all(zh-cn,VS.110).gif说明

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

ms235216.collapse_all(zh-cn,VS.110).gif代码

// 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
}

请参见

其他资源

编程在Visual C++的.NET