Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

The Debug Class in Managed Extensions for C++

Visual Studio .NET 2003

When using the .NET Framework Debug Class in a Managed Extensions for C++ application, the behavior does not change between a debug and a release build.

Note   The behavior for the Trace Class 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.

For example, the following code always prints "test", regardless of whether you compile with /DDEBUG or not.

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

int main()
{
   TextWriterTraceListener * myWriter = new
      TextWriterTraceListener(System::Console::Out);
   Debug::Listeners->Add(myWriter);
   Debug::WriteLine("test");
}

Output

test

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:

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

int main()
{
   TextWriterTraceListener * myWriter = new
      TextWriterTraceListener(System::Console::Out);
   Debug::Listeners->Add(myWriter);
#ifdef DEBUG   // checks for a debug build
   Debug::WriteLine("test");
#endif   //ends the conditional block
}

See Also

Interoperability in Managed Extensions for C++

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.