The Debug and Trace classes allow you to produce and log informative messages about the conditions of your application without having to interrupt application execution. In this lesson, you will learn how to use the Debug and Trace classes, how to add Trace Listeners and Trace switches, and how to configure Trace switches in a program that has been compiled and released.
When debugging simple applications, you frequently do not need to perform any debugging tasks more complex than examining your code line-by-line as described in the previous lesson. Complex applications are another matter, however. Large programs can easily have hundreds of thousands of lines of code. When an erroneous result occurs, it might not be possible to track down logical errors by stepping through code and examining application variables.
The Debug class provides a way for you to create and log informative messages about program conditions while your application is executing. The Trace class allows you to create diagnostic instrumentation for your application that can deliver informative messages about program conditions even after your application has been compiled and released.
Tracing
The Trace and Debug classes are classes that contain Shared (static) methods that allow you to test conditions at run time and log the results. The output from these methods is displayed in the Output window and can be viewed while debugging. The output from these methods is also sent to the Listeners collection.
The Listeners collection contains a group of classes that can receive output from the Trace and Debug classes. Trace and Debug share the same Listeners collection, and all the listeners in the collection receive the same input from Trace and Debug. There are different kinds of Listeners, such as Listeners that write to text files and Trace Listeners that write to event logs.
The Trace and Debug classes are identical. They expose the same methods and properties, and write output to the same set of Listeners. The only difference between Trace and Debug is that Debug statements are not included by default when the program is compiled into a release build, whereas Trace statements are. Thus, the Debug class is used principally for debugging in the development phase, whereas Trace can be used for testing and optimization after an application has been compiled and released.
Writing Trace and Debug Output
You can write output to the Listeners collection by using the methods exposed by the Trace and Debug classes. These classes expose six methods for writing output:
Write.
Writes text to the Listeners collection unconditionally.
WriteLine.
Writes text to the Listeners collection and follows the text with a carriage return.
WriteIf.
Writes text to the Listeners collection if the specified Boolean expression is true.
WriteLineIf.
Writes text and a carriage return to the Listeners collection if the specified Boolean expression is true.
Assert.
Writes an assertion message to the Listeners collection if the specified Boolean expression is false. It also causes a message box with the assertion message to be displayed.
Fail.
Creates an assertion that automatically fails without testing a condition. An assertion message is written to the Listeners collection, and a message box is displayed with the message.
You can alter the indentation level of Trace messages as they are written to the Listeners collection by calling the Indent and Unindent methods, and by setting the IndentSize and IndentLevel properties. These methods and properties are useful for creating hierarchical displays of error messages.
The IndentSize property gets or sets the number of spaces in a single indent, and the indent level gets or sets the number of indents currently being applied. The Indent method increases the IndentLevel by one, and the Unindent method decreases the IndentLevel by one.
To write output using Trace or Debug
If necessary, set the indent level to the appropriate position by using the Indent and Unindent methods.
Call the appropriate method to write output to the Listeners collection:
Call Write or WriteLine to unconditionally write Trace output.
Call WriteIf or WriteLineIf to test a condition and write output if the condition is true.
Call Fail to unconditionally write Trace output and display a message box.
Call Assert to test a condition and write output and display a message box if the condition proves false
The Listeners Collection
All output from the Trace and Debug classes is directed to the Listeners collection. The Listeners collection is a specialized collection that organizes and exposes classes that are capable of receiving Trace output. Each member of the Listeners collection receives any and all output from the Trace and Debug classes. How that output is handled is then up to the listener.
The Listeners collection is initialized with one member:
an instance of the DefaultTraceListener class. The DefaultTraceListener, as the name implies, is automatically created and will receive Trace and Debug output even if no other Listeners are attached. Trace output received by the DefaultTraceListener is directed to the debugger. It is through the DefaultTraceListener that Trace output is displayed in the Output window in Visual Studio.
If you want to create a log of Trace messages that you can refer to separately from the debugger, you must add at least one more Listener. The Microsoft .NET Framework base class library provides two classes that can log Trace output: the EventLogTraceListener class and the TextWriterTraceListener class. Both classes inherit from the .NET Framework MustInherit (abstract) class TraceListener.
Logging Trace Output to Text
The TextWriterTraceListener class writes its output as text, either to a Stream object or to a TextWriter object. For example, the Console.Out property is a TextWriter. Thus, you could create a TextWriterTraceListener that writes all of its output to the Console window.
Another use of Stream and TextWriter objects is writing to text files. To create a TextWriterTraceListener that writes Trace output to a text file, you must first create or open the file that will be used to record the output. Next, you need to create an instance of the TextWriterTraceListener that specifies that file as its target. In addition, you need to add the TextWriterTraceListener to the Listeners collection.
You can use the EventLogTraceListener to log Trace output to an EventLog object. The method for doing this is basically the same as logging to a text file. You create a new EventLog or specify an already existing event log, create a new EventLogTraceListener, and add it to the Listeners collection. Trace output will then be logged to the specified EventLog as EventLogEntry objects.
When debugging, you will usually want to receive all of the output from your Debug and Trace statements. After an application has been compiled and released, however, you will usually want to enable tracing only in special cases.
Trace switches are configurable switches that you can use to set whether or not Trace statements are displayed. Trace switches can be configured within the application by altering the application configuration file after it has been compiled and distributed.
The .NET Framework base class library contains two kinds of Trace switches. Instances of the BooleanSwitch class return a Boolean value. Thus, they are either on or off, like a toggle switch. The TraceSwitch class allows the user to set the level represented by the switch to one of five settings depending on the kind of output the user would like to receive.
You create an instance of a BooleanSwitch or a TraceSwitch just as you would any other class. Both types of switches require two parameters in their constructor: a DisplayName, which is the name that the switch will be called in the user interface, and a Description, which contains a short description of the switch.
The TraceSwitch class has five settings that represent different levels of errors. These settings are exposed by the TraceSwitch.Level property. This property can be set to any of five different values represented by the TraceLevel enum. They are as follows:
TraceLevel.Off represents a TraceSwitch that is not currently active. The integer value is 0.
TraceLevel.Error only represents very brief error messages. The integer value is 1.
TraceLevel.Warning represents error messages and warnings. The integer value is 2.
TraceLevel.Info represents error messages, warnings, and short informative messages.
The integer value is 3.
TraceLevel.Verbose represents error messages, warnings, and detailed descriptions of program execution. The integer value is 4.
Additionally, the TraceSwitch class exposes four read-only Boolean properties that represent these different trace levels. These properties are:
TraceSwitch.TraceError
TraceSwitch.TraceWarning
TraceSwitch.TraceInfo
TraceSwitch.TraceVerbose
These read-only properties correspond to the trace levels of the same names. When the TraceSwitch.Level property is set, these four properties are automatically set to the appropriate level. For example, if the TraceSwitch.Level property is set to TraceLevel.Info, the TraceSwitch.TraceInfo property will return true. Additionally, TraceSwitch.TraceError and TraceSwitch.TraceWarning will also return true. When a specified level is set, it and all lower levels return true.
To create and use Trace switches
Create an instance of either the BooleanSwitch class or the TraceSwitch class, as appropriate. You must supply the DisplayName parameter and the Description parameter.
Test the status of the switches in code by using the Trace.WriteIf and Trace.WriteLineIf methods.
Configuring Trace Switches
Trace switches can be turned on and off after your application has been compiled and distributed. Trace switches are configured by manipulating the application .config file, which is an XML file that can contain information for the application. The .config file must be located in the same folder as the executable and must have the name (application name).exe.config.
Not all applications have a .config file, so if you are using Trace switches, you might need to create one for your program. When your application executes code that creates a Trace switch, it checks the .config file for information about that switch. This file is only examined once per switch. If you want to change any settings after a particular Trace switch has been created, you must stop the application, make changes to the .config file, and then restart your application.
When you create an instance of a Trace switch, one of the parameters you must supply is the DisplayName. This is the name that is used to configure the Trace switch in the .config file. When editing the .config file, you must specify not only the name of the switch but also the value to which it is to be set.
The value must be an integer. For BooleanSwitch objects, 0 represents off, and any nonzero value represents on. For TraceSwitch objects, the values 0, 1, 2, 3, and 4 correspond to TraceLevel.Off, TraceLevel.Error, TraceLevel.Warning, TraceLevel.Info, and TraceLevel.Verbose, respectively. Any value greater than 4 is treated as TraceLevel.Verbose.
related post
DAY 11 OOPS INTRODUCTION
DAY 12 POLYMORPHISM
DAY 13 INHERITANCE AND POLYMORPHISM
DAY 14 EBUGGING TOOLS IN DOT NET
DAY 15 DEBUG AND TRACE IN CLASSES
DAY 16 UNIT TEST PLAN
DAY 17 EXCEPTIONS IN VISUAL STUDIO
DAY 19 ADO.NET INTRODUCTION
DAY 20 DATA ACCESSING IN DOT NET
DAY 21 DATA BASE OBJECTS
When debugging simple applications, you frequently do not need to perform any debugging tasks more complex than examining your code line-by-line as described in the previous lesson. Complex applications are another matter, however. Large programs can easily have hundreds of thousands of lines of code. When an erroneous result occurs, it might not be possible to track down logical errors by stepping through code and examining application variables.
The Debug class provides a way for you to create and log informative messages about program conditions while your application is executing. The Trace class allows you to create diagnostic instrumentation for your application that can deliver informative messages about program conditions even after your application has been compiled and released.
Tracing
The Trace and Debug classes are classes that contain Shared (static) methods that allow you to test conditions at run time and log the results. The output from these methods is displayed in the Output window and can be viewed while debugging. The output from these methods is also sent to the Listeners collection.
The Listeners collection contains a group of classes that can receive output from the Trace and Debug classes. Trace and Debug share the same Listeners collection, and all the listeners in the collection receive the same input from Trace and Debug. There are different kinds of Listeners, such as Listeners that write to text files and Trace Listeners that write to event logs.
The Trace and Debug classes are identical. They expose the same methods and properties, and write output to the same set of Listeners. The only difference between Trace and Debug is that Debug statements are not included by default when the program is compiled into a release build, whereas Trace statements are. Thus, the Debug class is used principally for debugging in the development phase, whereas Trace can be used for testing and optimization after an application has been compiled and released.
Writing Trace and Debug Output
You can write output to the Listeners collection by using the methods exposed by the Trace and Debug classes. These classes expose six methods for writing output:
Write.
Writes text to the Listeners collection unconditionally.
WriteLine.
Writes text to the Listeners collection and follows the text with a carriage return.
WriteIf.
Writes text to the Listeners collection if the specified Boolean expression is true.
WriteLineIf.
Writes text and a carriage return to the Listeners collection if the specified Boolean expression is true.
Assert.
Writes an assertion message to the Listeners collection if the specified Boolean expression is false. It also causes a message box with the assertion message to be displayed.
Fail.
Creates an assertion that automatically fails without testing a condition. An assertion message is written to the Listeners collection, and a message box is displayed with the message.
You can alter the indentation level of Trace messages as they are written to the Listeners collection by calling the Indent and Unindent methods, and by setting the IndentSize and IndentLevel properties. These methods and properties are useful for creating hierarchical displays of error messages.
The IndentSize property gets or sets the number of spaces in a single indent, and the indent level gets or sets the number of indents currently being applied. The Indent method increases the IndentLevel by one, and the Unindent method decreases the IndentLevel by one.
To write output using Trace or Debug
If necessary, set the indent level to the appropriate position by using the Indent and Unindent methods.
Call the appropriate method to write output to the Listeners collection:
Call Write or WriteLine to unconditionally write Trace output.
Call WriteIf or WriteLineIf to test a condition and write output if the condition is true.
Call Fail to unconditionally write Trace output and display a message box.
Call Assert to test a condition and write output and display a message box if the condition proves false
The Listeners Collection
All output from the Trace and Debug classes is directed to the Listeners collection. The Listeners collection is a specialized collection that organizes and exposes classes that are capable of receiving Trace output. Each member of the Listeners collection receives any and all output from the Trace and Debug classes. How that output is handled is then up to the listener.
The Listeners collection is initialized with one member:
an instance of the DefaultTraceListener class. The DefaultTraceListener, as the name implies, is automatically created and will receive Trace and Debug output even if no other Listeners are attached. Trace output received by the DefaultTraceListener is directed to the debugger. It is through the DefaultTraceListener that Trace output is displayed in the Output window in Visual Studio.
If you want to create a log of Trace messages that you can refer to separately from the debugger, you must add at least one more Listener. The Microsoft .NET Framework base class library provides two classes that can log Trace output: the EventLogTraceListener class and the TextWriterTraceListener class. Both classes inherit from the .NET Framework MustInherit (abstract) class TraceListener.
Logging Trace Output to Text
The TextWriterTraceListener class writes its output as text, either to a Stream object or to a TextWriter object. For example, the Console.Out property is a TextWriter. Thus, you could create a TextWriterTraceListener that writes all of its output to the Console window.
Another use of Stream and TextWriter objects is writing to text files. To create a TextWriterTraceListener that writes Trace output to a text file, you must first create or open the file that will be used to record the output. Next, you need to create an instance of the TextWriterTraceListener that specifies that file as its target. In addition, you need to add the TextWriterTraceListener to the Listeners collection.
You can use the EventLogTraceListener to log Trace output to an EventLog object. The method for doing this is basically the same as logging to a text file. You create a new EventLog or specify an already existing event log, create a new EventLogTraceListener, and add it to the Listeners collection. Trace output will then be logged to the specified EventLog as EventLogEntry objects.
When debugging, you will usually want to receive all of the output from your Debug and Trace statements. After an application has been compiled and released, however, you will usually want to enable tracing only in special cases.
Trace switches are configurable switches that you can use to set whether or not Trace statements are displayed. Trace switches can be configured within the application by altering the application configuration file after it has been compiled and distributed.
The .NET Framework base class library contains two kinds of Trace switches. Instances of the BooleanSwitch class return a Boolean value. Thus, they are either on or off, like a toggle switch. The TraceSwitch class allows the user to set the level represented by the switch to one of five settings depending on the kind of output the user would like to receive.
You create an instance of a BooleanSwitch or a TraceSwitch just as you would any other class. Both types of switches require two parameters in their constructor: a DisplayName, which is the name that the switch will be called in the user interface, and a Description, which contains a short description of the switch.
The TraceSwitch class has five settings that represent different levels of errors. These settings are exposed by the TraceSwitch.Level property. This property can be set to any of five different values represented by the TraceLevel enum. They are as follows:
TraceLevel.Off represents a TraceSwitch that is not currently active. The integer value is 0.
TraceLevel.Error only represents very brief error messages. The integer value is 1.
TraceLevel.Warning represents error messages and warnings. The integer value is 2.
TraceLevel.Info represents error messages, warnings, and short informative messages.
The integer value is 3.
TraceLevel.Verbose represents error messages, warnings, and detailed descriptions of program execution. The integer value is 4.
Additionally, the TraceSwitch class exposes four read-only Boolean properties that represent these different trace levels. These properties are:
TraceSwitch.TraceError
TraceSwitch.TraceWarning
TraceSwitch.TraceInfo
TraceSwitch.TraceVerbose
These read-only properties correspond to the trace levels of the same names. When the TraceSwitch.Level property is set, these four properties are automatically set to the appropriate level. For example, if the TraceSwitch.Level property is set to TraceLevel.Info, the TraceSwitch.TraceInfo property will return true. Additionally, TraceSwitch.TraceError and TraceSwitch.TraceWarning will also return true. When a specified level is set, it and all lower levels return true.
To create and use Trace switches
Create an instance of either the BooleanSwitch class or the TraceSwitch class, as appropriate. You must supply the DisplayName parameter and the Description parameter.
Test the status of the switches in code by using the Trace.WriteIf and Trace.WriteLineIf methods.
Configuring Trace Switches
Trace switches can be turned on and off after your application has been compiled and distributed. Trace switches are configured by manipulating the application .config file, which is an XML file that can contain information for the application. The .config file must be located in the same folder as the executable and must have the name (application name).exe.config.
Not all applications have a .config file, so if you are using Trace switches, you might need to create one for your program. When your application executes code that creates a Trace switch, it checks the .config file for information about that switch. This file is only examined once per switch. If you want to change any settings after a particular Trace switch has been created, you must stop the application, make changes to the .config file, and then restart your application.
When you create an instance of a Trace switch, one of the parameters you must supply is the DisplayName. This is the name that is used to configure the Trace switch in the .config file. When editing the .config file, you must specify not only the name of the switch but also the value to which it is to be set.
The value must be an integer. For BooleanSwitch objects, 0 represents off, and any nonzero value represents on. For TraceSwitch objects, the values 0, 1, 2, 3, and 4 correspond to TraceLevel.Off, TraceLevel.Error, TraceLevel.Warning, TraceLevel.Info, and TraceLevel.Verbose, respectively. Any value greater than 4 is treated as TraceLevel.Verbose.
related post
DAY 11 OOPS INTRODUCTION
DAY 12 POLYMORPHISM
DAY 13 INHERITANCE AND POLYMORPHISM
DAY 14 EBUGGING TOOLS IN DOT NET
DAY 15 DEBUG AND TRACE IN CLASSES
DAY 16 UNIT TEST PLAN
DAY 17 EXCEPTIONS IN VISUAL STUDIO
DAY 19 ADO.NET INTRODUCTION
DAY 20 DATA ACCESSING IN DOT NET
DAY 21 DATA BASE OBJECTS
No comments:
Post a Comment