Compiling Conditionally with Trace and Debug
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
- Right-click the project node in Solution Explorer.
- Choose Properties from the shortcut menu.
- In the Property Page dialog box that appears, click the Configuration Properties tab in the left pane and choose the Build option.
- 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.csTip To compile more than one application file, leave a blank space between the file names, for example,
The meaning of the conditional-compilation directives used in the above examples is as follows:
|Visual Basic compiler|
|References an external assembly (EXE or DLL)|
|Defines a conditional compilation symbol|
Note You must spell TRACE or DEBUG with uppercase letters. For more information about the conditional compilation commands, entervbc /?(for Visual Basic) orcsc /?(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.
Language Statement Result Visual Basic #CONST TRACE = true Enables tracing #CONST TRACE = false Disables tracing #CONST DEBUG = true Enables debugging #CONST DEBUG = false Disables debugging C# #define TRACE Enables tracing #undefine TRACE Disables tracing #define DEBUG Enables debugging #undefine DEBUG Disables 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.
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