Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

Runtime Debugging

CHAPTER 31

LISTING 31.9 TraceSwitch Entry in Config File: TraceSwitchDemo.config

<configuration>

<system.diagnostics>

<switches>

<add name=”TraceOutput” value=”3” /> </switches>

</system.diagnostics>

</configuration>

LISTING 31.10 Compilation Instructions for Listing 31.8

csc /d:TRACE TraceSwitchDemo.cs

Making Assertions

Another common debugging task is to check the state of a program at various intervals for logical consistency. This is performed with the Debug.Assert() method. By sprinkling Assert() methods at strategic points in a routine, such as preconditions, intermediate state, and post-conditions, you can verify that routine’s logical consistency. Whenever the assertion proves false, a given message is displayed in the form of a message box. Listing 31.11 has a simple program demonstrating the mechanics of the Assert() method.

LISTING 31.11 Assertion Demonstration: AssertDemo.cs

using System;

using System.Diagnostics;

///<summary>

///Assertion Demonstration.

///</summary>

class AssertDemo

{

static void Main(string[] args)

{

decimal profit = -0.01m;

// do some calculations

Debug.Assert(profit >= 0.0m,

“Illogical Negative Profit Calculation”);

}

}

643

31

UNTIMER

EBUGGINGD

644

Extreme C#

PART IV

The example in Listing 31.11 simulates some fictitious profit calculation that should never return a negative result. The Debug.Assert() method takes two parameters. The first is the logical condition to check, which should evaluate to a Boolean true or false. In this case, it’s making sure the profit is always zero or greater. The second parameter is the message to be displayed. The example forces the assertion to evaluate to false, displaying the message shown in Figure 31.1.

Assertions are designed to work only in debugging mode. Therefore, you will want to add a /define switch to the command-line when debugging. This program can be compiled with the command line in Listing 31.12.

LISTING 31.12 Compilation Instructions for Listing 31.11

csc /d:DEBUG AssertDemo.cs

FIGURE 31.1

Assertion message box.

Summary

Once appropriate statements and methods are in place, runtime debugging can make program verification more efficient by watching console printouts of viewing log files for pertinent results. Runtime debugging can be turned on and off with conditional attributes, specialized output methods that accept Boolean parameters, command line options, and preprocessing directives.

The Debug class is effective in development environments where the debugging code will be removed for deployment. Alternatively, the Trace class would be the best decision for situations where code should be deployed with a debugging capability.

Runtime debugging in trace-enabled code can be controlled with Boolean switches or multilevel trace switches. Each option provides a means of controlling the level of debugging with less disruption to a customer.

The Debug.Assert() method assists in verifying the logical consistency of an application during debugging. When a specified constraint fails, the Assert() method notifies the user with a message box displaying information about the reason for the failure.

Runtime Debugging

CHAPTER 31

Runtime detection of program errors is an important capability. Similarly, it’s important to monitor the performance of a program. The next chapter, “Performance Monitoring,” shows how to capture runtime performance of an application.

645

31

UNTIMER

EBUGGINGD