Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Compiling Conditionally with Trace and Debug

Compiling Conditionally with Trace and Debug

Visual Studio .NET 2003

While you are debugging an application during development, both your tracing and debugging output go to the Output window in Visual Studio. However, to include tracing features in a deployed application, you must compile your instrumented applications with the TRACE compiler directive enabled. This allows tracing code to be compiled into the release version of your application. If you do not enable the TRACE directive, all tracing code is ignored during compilation and is not included in the executable code that you will deploy.

Both the tracing and debugging methods have associated conditional attributes. For example, if the conditional attribute for tracing is true, all trace statements are included within an assembly (a compiled .exe file or .dll); if the Trace conditional attribute is false, the trace statements are not included.

You can have either the Trace or Debug conditional attribute turned on for a build, or both, or neither. Thus, there are four types of build: Debug, Trace, both, or neither. Some release builds for production deployment might contain neither; most debugging builds contain both.

You can specify the compiler settings for your application in several ways:

  • The property pages
  • The command line
  • #CONST (for Visual Basic) and #define (for C#)

To change compile settings from the property pages dialog box

  1. Right-click the project node in Solution Explorer.
  2. Choose Properties from the shortcut menu.
  3. In the Property Page dialog box that appears, click the Configuration Properties tab in the left pane and choose the Build option.
  4. Choose the desired compiler settings.
    • In Visual Basic, select the check boxes for the compiler settings you want to enable. Clear the check boxes for settings you want to disable.
    • In C#, in the Conditional Compilation Constants field, type the name of the setting you want to enable.
      Visual C# Note   To enable both Debug and Trace, type DEBUG;TRACE (or TRACE;DEBUG). Delete the name of any setting you want to disable. C# is case-sensitive, and the setting names must be uppercase.

To compile instrumented code using the command line

  • Set a conditional compiler switch on the command line. The compiler will include trace or debug code in the executable.

    For example, the following compiler instruction entered on the command line would include your tracing code in a compiled executable:

    For Visual Basic: vbc /r:System.dll /d:TRACE=TRUE /d:DEBUG=FALSE MyApplication.vb

    For C#: csc /r:System.dll /d:TRACE /d:DEBUG=FALSE MyApplication.cs

    Tip   To compile more than one application file, leave a blank space between the file names, for example,
    MyApplication1.vb MyApplication2.vb MyApplication3.vb or MyApplication1.cs MyApplication2.cs MyApplication3.cs.

The meaning of the conditional-compilation directives used in the above examples is as follows:

vbcVisual Basic compiler
cscC# compiler
/r:References an external assembly (EXE or DLL)
/d:Defines a conditional compilation symbol
Note   You must spell TRACE or DEBUG with uppercase letters. For more information about the conditional compilation commands, enter
vbc /?
(for Visual Basic) or
csc /?
(for C#) at the command prompt. For more information, see Building from the Command Line (C#) or Invoking the Command-Line Compiler (Visual Basic).

To perform conditional compilation using #CONST or #define

  • Type the appropriate statement for your programming language at the top of the source code file.
    Visual Basic#CONST TRACE = trueEnables tracing
     #CONST TRACE = falseDisables tracing
     #CONST DEBUG = trueEnables debugging
     #CONST DEBUG = falseDisables debugging
    C##define TRACEEnables tracing
     #undefine TRACEDisables tracing
     #define DEBUGEnables debugging
     #undefine DEBUGDisables debugging

To disable tracing or debugging

  • Delete the compiler directive from your source code.

    - or -

  • Comment out the compiler directive.
    Note   When you are ready to compile, you can either choose Build from the Build menu, or use the command line method but without typing the d: to define conditional compilation symbols.

See Also

Introduction to Instrumentation and Tracing | Tracing and Instrumenting Applications in Visual Basic and Visual C# | Configuring Trace Switches | Trace Switches | Trace Listeners | Adding Trace Statements to Application Code | Building from the Command Line | Invoking the Command-Line Compiler

© 2015 Microsoft