Debug 類別 (C++/CLI)

在 Visual C++ 應用程式中使用 Debug 時,行為不會在偵錯與發行組建之間變更。

備註

的行為與 Debug 類別的行為 Trace 相同,但相依于所定義的符號 TRACE。 這表示您必須有任何 #ifdef 追蹤相關程式碼,才能防止發行組建中的偵錯行為。

範例:一律執行輸出語句

描述

不論您使用 /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

範例:使用 #ifdef 和 #endif 指示詞

描述

若要取得預期的行為(也就是沒有針對發行組建列印的「測試」輸出),您必須使用 #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
}

另請參閱

以 C++/CLI 進行 .NET 程式設計 (Visual C++)