Enterprise Library—Lab Exercises



Adding Tracing - Step 1

    Often you would like to time sections of our application.  The Logging and Instrumentation Application Block includes tracing which allows us to book-end  a section of code and log the execution time.

     

    Open to the Enterprise Library Configuration Tool by selecting Windows Start menu, select All Programs | Microsoft patterns and practices | Enterprise Library | Enterprise Library Configuration, and open the App.config file.
    Set TracingEnabled to True in the Client Settings of the Logging and Instrumentation Application Block configuration section.

    Select the File | Save All menu command to save the configuration. In Visual Studio, open the Calculator.cs file in the EnoughPI project, and add the following namespace.

    Adding trace code - Step 2

    using Microsoft.Practices.EnterpriseLibrary.Logging.Tracing;

    Trace (for timing) the complete calculation by adding the following code to the Calculate method in the Calculator.csfile of the EnoughPI project.

     

    public string Calculate(int digits)
    {
        StringBuilder pi = new StringBuilder("3", digits + 2);
        string result = null;
     
        try
        {
            if (digits > 0)
            {
                // TODO: Add Tracing around the calculation
                using (new Tracer(Category.Trace, "Calculate PI"))
                {
                    pi.Append(".");
                    for (int i = 0; i < digits; i += 9)
                    {
                        CalculatingEventArgs args;
                        args = new CalculatingEventArgs(pi.ToString(), i + 1);
                        OnCalculating(args);
     
                        // Break out if cancelled
                        if (args.Cancel == true) break;
     
                        // Calculate next 9 digits
                        int nineDigits = NineDigitsOfPi.StartingAt(i+1);
                        int digitCount = Math.Min(digits - i, 9);
                        string ds = string.Format("{0:D9}", nineDigits);
                        pi.Append(ds.Substring(0, digitCount));
                    }
                }              
            }
     
            result = pi.ToString();
     
            // Tell the world I've finished!
            OnCalculated(new CalculatedEventArgs(result));
        }
        catch (Exception ex)
        {
            // Tell the world I've crashed!
            OnCalculatorException(new CalculatorExceptionEventArgs(ex));
        }
     
        return result;
    }
    Why enclose the Tracer in a using block?  The Tracer will stop timing, and log its end trace message, when it is disposed.  The using block guarantees us that Dispose() will be called on the Tracer at the end of the block.  Allowing the Garbage Collector to dispose of the Tracer will result in incorrect timings.

    Run the application, and perform a calculation. Close the application.

    The Trace has been configured to write to the trace.log file via the Flat File Sink.  View the elapsed time in the end trace message.

    Close Visual Studio.