Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Debug Class (C++/CLI)

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Debug Class (C++/CLI).

When using Debug in a Visual C++ application, the behavior does not change between a debug and a release build.

The behavior for Trace is identical to the behavior for the Debug class, but is dependent on the symbol TRACE being defined. This means that you must #ifdef any Trace-related code to prevent debug behavior in a release build.

Description

The following sample always executes the output statements, regardless of whether you compile with /DDEBUG or /DTRACE.

Code

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

Output

    Entering Main  
Hello World.  
    Exiting Main  
test  

Description

To get the expected behavior (that is, no "test" output printed for a release build), you must use the #ifdef and #endif directives. The previous code sample is modified below to demonstrate this fix:

Code

// 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 Programming with C++/CLI (Visual C++)

Show:
© 2017 Microsoft