Take Advantage of the Logging Block in Enterprise Library 3.0

Adding reusable, configurable, and robust logging to your applications can't get much easier.

by Thiru Thangarathinam

Dec 6, 2007

Page 1 of 2

ost applications need to write log messages at different layers of the application for instrumentation, debugging, or monitoring purposes. Generally, developers write code to log these messages to an external store such as an event log, a database, an external flat file, or an XML file. Logging typically requires a lot of reusable boilerplate code to construct an effective application logging functionality. Fortunately, you no longer have to create logging code manually. The patterns and practices team at Microsoft, realizing the need for a reusable logging capability, created the logging block and made it an integral component of Enterprise Library 3.0. This article introduces the logging block by providing and explaining examples that use it to write robust and fault-tolerant .NET applications.

The logging block provides a flexible and extensible logging mechanism that you can use in any type of .NET application—including Windows Forms applications, ASP.NET applications, console applications, and so on. By default, it ships with a number of reusable logging classes (also known as trace listeners) that can store log information to a wide range of external sources, including databases, XML files or flat files, MSMQ, a specified event log, and so on.

As a developer enabling basic logging functionalities, you just need to choose a trace listener and declaratively specify your choice in the application's configuration file; then you can make logging calls from the application—the rest is all handled for you.

Figure 1. Logging Block Dependencies: The trace listeners register the log entries to the appropriate log destinations. In addition to the core assemblies, the logging block also references the data access block for logging data in a database.

To be able to use the basic features of logging block, you need to reference the Microsoft.Practices.EnterpriseLibrary.Common.dll, and Microsoft.Practices.EnterpriseLibrary.Logging.dll assemblies. If you intend to log information to a database, you also need to reference the Microsoft.Practices.EnterpriseLibrary.Data.dll assembly. Figure 1 shows the dependencies of the logging block.

Build a Simple Example
Here's a simple example that explores the steps involved in configuring the logging block to log messages to the event log. To begin, create a new Visual C# Windows Application named LoggingBlockExample. After creating the project, add references to the Microsoft.Practices.EnterpriseLibrary.Common.dll and Microsoft.Practices.EnterpriseLibrary.Logging.dll assemblies by navigating to the <DriveName>:\Program Files\Microsoft Enterprise Library 3.1—May 2007\Bin folder.

After that, import the following core logging namespace:

using Microsoft.Practices.EnterpriseLibrary.Logging;

Next, add the configuration settings to the application configuration (app.config) file to specify the event log as the logging destination (see Listing 1). All the required logging entries reside within the <loggingConfiguration> section, in four subsections:

listeners—Lets you specify the trace listeners to which log entries are routed.

formatters—This optional element allows you to specify the format of the log entry produced by the format-aware trace listeners.

categorySources—Allows you to specify the sources that are responsible for mapping specific categories to their corresponding trace listeners. Note that one of the categories in the <categorySources> element is referenced in the defaultCategory attribute in the <loggingConfiguration> element.

specialSources—Lets you specify special trace sources that can process specific types of log entries.

With the app.config file settings in place, the next step is to write code against the logging block.
First, import the core logging namespace as shown below:

using Microsoft.Practices.EnterpriseLibrary.Logging;

Next, open the Windows form, add a command button named btnLogMessage, and modify its Click event handler so it matches the following code:

Figure 2. Event Log Message: Output such as this is typical for logged messages viewed through the Application category of the Windows Event Viewer.

//Note that the default category is General
entry.Categories.Add("General");
Logger.Write(entry);
MessageBox.Show("Log is written");
}

The preceding code uses two important logging classes: LogEntry and Logger. LogEntry represents a single log entry, so the class exposes properties such as the priority of the log message, event id, log message, and so on. Logger exposes methods for writing the log message to one or more trace listeners as identified in the configuration file.

To write a log message, you create a LogEntry instance and populate its properties appropriately. A LogEntry lets you specify one or many categories under which the message may be logged through the appropriate trace listeners.

Figure 2 shows the log entry in the event log when you click on the "Log Message" button in the sample application.