Lab 5-3: Implementing Tracing

Lab 5-3: Implementing Tracing

In this lab, you will add tracing functionality to your application. You will add a Trace switch and Trace statements to your application that log usage of division and the number of times the user attempts to divide by zero. You will log Trace output to a text file, and enable and disable tracing from the application s configuration file. The solution to this lab is available on the Supplemental Course Materials CD-ROM in the \Labs\Ch05\Lab 5-3\Solution folder.

Before You Begin

You should have completed Lab 5-2 or loaded the Lab 5-2 solution from the Supplemental Course Materials CD-ROM.

Estimated lesson time: 20 minutes

Exercise 5-3.1: Adding Trace Functionality

In this exercise, you will add code statements to implement Trace functionality in your application. You will create a new Trace Listener that records Trace output to a text file. You will add a BooleanSwitch to your application that determines whether Trace output is produced, and you will add Trace statements to write output to the Listeners collection.

To add Trace functionality

  1. In the designer, double-click Form1 to open the code editor to the Form.Load event handler.

  2. For Visual C# only, add the following line at the top of the code editor with the other using statements:

    Visual C#

    using System.Diagnostics;

  3. In the Form.Load event handler, add code to declare a new TextWriterTraceListener and add it to the Listeners collection. An example is shown below:

    Visual Basic .NET

    Dim myListener As New TextWriterTraceListener("C:\myTraceLog.txt") Trace.Listeners.Add(myListener)

    Visual C#

    TextWriterTraceListener myListener = new TextWriterTraceListener("C:\\myTraceLog.txt"); Trace.Listeners.Add(myListener);

  4. Outside of any method and near the top of your class definition, declare a global BooleanSwitch as shown below:

    Visual Basic .NET

    Public myBooleanSwitch As New BooleanSwitch("myBooleanSwitch", _ "This switch controls tracing in this application")

    Visual C#

    public BooleanSwitch myBooleanSwitch = new BooleanSwitch("myBooleanSwitch", "This switch controls tracing in this application");

  5. In the OperationHandler and EqualsHandler methods, locate the Catch block that you added in the previous lab. Add the following lines to these Catch blocks beneath the MessageBox.Show statement:

    Visual Basic .NET

    Trace.WriteLineIf(myBooleanSwitch.Enabled, _ "User attempted to Divide by Zero") Trace.Flush()

    Visual C#

    Trace.WriteLineIf(myBooleanSwitch.Enabled,  "User attempted to Divide by Zero"); Trace.Flush();

  6. In the Finally blocks that you created in the previous lab, add the following lines:

    Visual Basic .NET

    Trace.WriteLineIf(myBooleanSwitch.Enabled, _  "Division operation attempted") Trace.Flush()

    Visual C#

    Trace.WriteLineIf(myBooleanSwitch.Enabled,  "Division operation attempted"); Trace.Flush();

  7. From the Build menu, choose Build All to save your work.

Exercise 5-3.2: Creating a .config file and testing tracing

In this exercise you will create a .config file for your application and add XML code to enable myBooleanSwitch in your application. You will then test the race functionality in your application.

  1. Create a configuration file for your application by the method appropriate to the language you are using.

    • For Visual Basic .NET and Visual C# 2003: From the Project menu, choose Add New Item. In the Add New Item Dialog Box, choose Application Configuration File.

    • For Visual C# 2002: From the Project menu, choose Add New Item. In the Add New Item Dialog Box, choose Text File. Rename the new text file App.config and add the following XML code:

      <?xml version="1.0" encoding="utf-8" ?> <configuration>

  2. In between the <configuration> and <\configuration> tags, add the following XML code to activate myBooleanSwitch:

    <system.diagnostics> <switches> <add name="myBooleanSwitch" value="1" /> </switches> </system.diagnostics>

  3. Press F5 to run your application. Try a few division operations with the calculator application, including a few attempts to divide by zero.

  4. From the Debug menu, choose Stop Debugging to stop the application. Examine the Output window. Note that Trace output has been directed to the Output window, which is the default Trace listener.

  5. Open C:\myTraceLog.txt with Notepad. Note that Trace output has been logged to this file also.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net