Enterprise Library—Lab Exercises



How to: Use the Enterprise Library Logging and Instrumentation Application Block.

Run the Enterprise Library Configuration tool and use file open to load the app.config file for the app you want to configure to use logging and tracing.

Right click on the Application and select New | Logging and Instrumentation Application Block.

 
The default Logging and Instrumentation Application Block Client Settings define an in-process distribution strategy and specify that logging is enabled. Note that trace is disabled by default and that tracing is not affected by the TRACE setting.

The default Logging and Instrumentation Application Block Distributor Settings define two Categories (General, and Trace).  Categories are text tags that you may apply to your log events to group them.  The General category defines one destination, Event Log Destination, which pairs the Event Log Sink with the Text Formatter.  The Trace category defines on destination, Flat File Destination, which pairs the Flat File Sink with the Text Formatter.
New categories may be added by right clicking on Categories and selecting New | Category.  A category may have many destinations.  A destination defines a log formatter and log sink pair.  Each formatter and sink many be used in many destinations.

Select the File | Save All menu command to save the configuration.

Note two logging configuration files (loggingconfiguration.config and loggingdistributorconfiguration.config) have been created.

The logging configuration files must be copied to the target compilation directory.  Select the Project | Properties menu command to view the Property Pages.  Select Common Properties | Build Events and add the following code to the Post-build Event Command Line.
copy "$(ProjectDir)\*.config" "$(TargetDir)"

Unless specified otherwise, the configuration section file is expected to be in the same directory as the application.  Hence you must copy the configuration section file to the compilation target directory

Adding Logging

In Visual Studio add a reference to the logging assembly:
 
  • Microsoft.Practices.EnterpriseLibrary.Logging.dll
    (Contains the logging application block base classes).
Add the following namespace to the class file you want to add logging to.
using Microsoft.Practices.EnterpriseLibrary.Logging;
 

Example using the logger to write the log entry to the log Distributor (to send formatted log entries to the category destination).

protected void OnCalculated(CalculatedEventArgs args)
{
    LogEntry log = new LogEntry();
    log.Message = string.Format("Calculated PI to {0} digits", args.Digits);
    log.Category = Category.General; // user defined constant
    log.Priority = Priority.Normal;       //  user defined constant
 
    Logger.Write(log);
 
    if (Calculated != null)
        Calculated(this, args);
}
 
Handling exceptions:
protected void OnCalculatorException(CalculatorExceptionEventArgs args)
{
    if (!(args.Exception is ConfigurationException))
    {
        Logger.Write(args.Exception, Category.General, Priority.High);     }
 
    if (CalculatorException != null)
        CalculatorException(this, args);
}
Notes:
  • You must test that the exception type is not a ConfigurationException as you would not be able to use the Logger if it has not be correctly configured.
  • You would normally use the Enterprise Library Exception Handling Application Block to create a consistent strategy for processing exceptions.

Run the Event Viewer.  From the Windows Start menu select Administrative Tools l Event Viewer.  View the Application log for messages from the Enterprise Library Logging source.