The .NET Framework provides two classes in the System.Diagnostics namespace, Debug and Trace, which help debug your application. These classes, mentioned in Section 184.108.40.206 and described in Chapter 21, enable several debugging techniques, such as assertions, conditional error messages, and capturing errors to a log file. The Debug and Trace classes have identical methods and properties.
To use tracing or debugging, you must compile the application with the appropriate flag. To include Debug statements in a C# program, include /d:DEBUG in the compile command line or add #define DEBUG as the first line in the source code file. To include Trace statements in a C# program, include /d:TRACE in the compile command line or add #define TRACE as the first line in the source code file.
To include Debug statements in a VB.NET program, include /d:DEBUG=True in the compile command line. To include Trace statements in a VB.NET program, include /d:TRACE=True in the compile command line.
In Visual Studio .NET, these flags are set by default by the Configuration Manager, described shortly. They can also be set explicitly. In C#, this is done by right-clicking on the project in the Solution Explorer and viewing the Property Pages dialog box for the project (not to be confused with the Properties window), and then going to the Configuration Properties Build page. The Conditional Compilation Constants are listed under Code Generation. In VB.NET, the procedure is the same, except that two checkboxes are on the Configuration Properties Build page, labeled Define DEBUG constant and Define TRACE constant.
The Configuration Manager in Visual Studio .NET provides an easy way to control the build process. By default, the Configuration Manager contains two different configurations: Debug and Release. You can quickly select between the two using the Configuration Manager drop-down menu shown in Figure 22-11.
Figure 22-11. Configuration Manager drop-down menu
The main difference between the two configurations is that Debug statements are included only in compiled Debug builds, not Release builds, while Trace statements are included in both. All the differences between Debug and Release builds are listed in Table 22-3.
Debug and Trace statements included in compiled build.
Only Trace statements included in compiled build.
Not optimized for speed.
Optimized for speed.
Enables incremental build.
Disables incremental build.
C# output goes into binDebug directory.
C# output goes into binRelease directory.
VB.NET output goes into bin directory.
VB.NET output goes into bin directory.
Notice that in C# projects, Debug and Release builds go into separate bin directories, while in VB.NET, both go into the same bin directory. You can change the output path in either language for any specific configuration by right-clicking on the project in the Solution Explorer and selecting Properties. This opens the Property Pages dialog box, shown in Figure 22-12 for C#. The VB.NET version of the dialog box is formatted somewhat differently but is functionally the same. Any changes made to a configuration apply only to this project.
Figure 22-12. Project Property pages dialog box for C#
You can specify which projects within a solution will be built for any specific configuration by opening the Configuration Manager dialog box, shown in Figure 22-13. This dialog box can be accessed from the configuration drop-down menu shown in Figure 22-11, from the Property Pages dialog box shown in Figure 22-12, or from the Build menu.
Figure 22-13. Configuration Manager
The Active Solution Configuration drop-down menu lets you select the active configuration and create new configurations or edit existing configurations. You can control which projects get built for each configuration by checking or unchecking the boxes in the Build column.
Windows Forms and the .NET Framework
Visual Studio .NET
Controls: The Base Class
Text and Fonts
Drawing and GDI+
Labels and Buttons
Other Basic Controls
TreeView and ListView
Date and Time Controls
Menus and Bars
Exceptions and Debugging
Configuration and Deployment