Software Development

Logging via the Microsoft Enterprise Library

The Logging and Instrumentation Application Block contained in Microsoft's Enterprise Library makes it easy to integrate logging in your project. Learn how to utilize the library's application blocks to enhance your project.

The Microsoft Enterprise Library was introduced in last week's column. It provides a collection of seven application blocks that utilize similar design patterns as well as development and configuration requirements. These blocks may be used to enhance your application while reducing development time. Today, we take a closer look at the library by putting it to work in a sample application.

Goals of the application blocks

The application blocks were designed to be used together—they often rely on each other. For example, the Logging and Instrumentation Application Block depends on the following on the Configuration Application Block to read configuration information. Also, the Common library functionality is utilized to provide various functions for exposing system management events and data. The Data Access Application Block may be required if the database event sink is used for logging.

Weekly .NET tips in your inbox
TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET.
Automatically sign up today!

Microsoft lays out the following goals for the Logging and Instrumentation Application Block:

  • Ensure using the application block is straightforward. This means the code used to work with it is clear and simple.
  • Provide a simple and intuitive object model.
  • Encapsulate logic used to perform the most common application logging and instrumentation tasks.
  • Present a standard consistent model for common logging and instrumentation tasks.
  • Minimize the need for custom logging and instrumentation-related code.
  • Ensure the application block is easily and highly configurable as well as extensible.
  • Ensure the performance impact is minimal or non-existent when compared to manually written logging code that provides the same functionality.

The basic goal is to make it easy to incorporate logging in your project. Let's take a closer look at putting it to work.

Logging

There are four basic concepts in the Logging and Instrumentation Application Block: log entries, tracers, sinks, and formatters.

  • Log entries allow you to record or write some information to a log sink.
  • A tracer event may be used to group log entries together. That is, a log entry may be part of a process.
  • A sink defines where and how log entries are recorded. A sink has many options: event log, MSMQ (Microsoft Message Queue), text file, WMI log, database entry (will need the database application block), and e-mail sent via SMTP.
  • A formatter is used to convert a log entry's information into a string.

There are numerous classes contained in the Logging and Instrumentation Application Block. The following list contains a sampling of the classes:

  • LogEntry: Represents a log message. It contains the common properties that are required for all log messages.
  • LoggingException: Used to handle logging exceptions.
  • Logger: A facade for writing log entries to log sinks. It writes entries to the default sink unless otherwise specified.

These classes are contained within the Microsoft.Practices.EnterpriseLibrary.Logging namespace. The application block includes additional namespaces for working with formatters, sinks, configuration, and so forth. The documentation included with the application block includes full details of each namespace.

Logging sample

With the basic concepts laid out, you can easily incorporate the application block in an application. The sample will only use the Logging namespace, so it must be referenced in your code. The following line makes it available in a C# application:

usingMicrosoft.Practices.EnterpriseLibrary.Logging;

And in VB.NET:

Imports Microsoft.Practices.EnterpriseLibrary.Logging

If you're using Visual Studio .NET, you should add a reference to the corresponding dll file (Microsoft.Practices.EnterpriseLibrary.Logging.dll). If you're not using Visual Studio, you can copy the necessary dll to the application's bin directory.

The following C# snippet adds logging to an application. It results in entries in the default log sink: Windows Event Log.

LogEntrylogEntry = new LogEntry();
logEntry.Message = "Starting pplication";
Logger.Write(logEntry);
Logger.Write("Shutting down application");

Here's the VB.NET equivalent:

Dim logEntry As LogEntry
logEntry = New LogEntry
logEntry.Message = "Starting up the application"
Logger.Write(logEntry)
Logger.Write("Shutting down the application")

Basically, the code writes to the log sink using two approaches. First, an instance of the LogEntry class is created. Its message property is populated—this appears with the entry in the log sink. The Write method of the Logger class is used to send the LogEntry object to the log sink. The final line uses a basic text message to make an entry in the log sink (event log in this case). You need to complete one final step before you can use this code in your application: You need to configure logging.

Enabling an application

The Enterprise Library uses the Enterprise Library Configuration Console, a graphical tool that allows changing and validating an application block's configuration settings (XML configuration file) by eliminating the difficulties that are encountered during manual settings. It is accessible via the Enterprise Library program group installed on your machine.

The console is used to create the configuration files necessary for your application to utilize one or more of the application blocks. The following steps are followed to create the files. In the Enterprise Library Configuration Console, follow these steps:

  1. Select File | New Application via the drop-down file menu. This adds a new application entry to the console.
  2. The new application appears as an entry in the console. It contains ConfigurationFile and Name properties accessible by clicking on it. You can edit these entries. The ConfigurationFile should point to your project's directory.
  3. Locate the new application and highlight it. Right-click on the new application and select Action | New | Logging And Instrumentation Application Block from the drop-down menu.
  4. The new application needs to be saved. Highlight it and select File | Save Application.
  5. Navigate to your project's directory (set via ConfigurationFile) and you'll see the configuration files. A Web application has a Web.config while app.config is used for all others. In addition, the project directory will contain two other configuration files: loggingConfiguration.config and loggingDistributorConfiguration.config.
  6. When running your application, the config files placed in the project directory must be copied to the bin directory to run properly (simply copy *.config to get everything).

With all configuration files in place, your code should run smoothly.

Starting point

We've covered the most basic aspects of logging with the Logging and Instrumentation Application Block; the application block provides many more features that may be utilized. You may choose to log events to a flat file, a SQL Server database, or expand it by developing your own custom log sink. In addition, the instrumentation features may be utilized. These are not enabled with the default Enterprise Library installation. The documentation details how to properly set it up.

Hopefully today's column has sparked your interest in the Enterprise Library. All of the library's application blocks may be utilized to enhance your application with well-tested code that takes advantage of best practices developed by Microsoft.

Miss a column?

Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

2 comments
cleverboy929
cleverboy929

Will this Microsoft.Practices.EnterpriseLibrary.Logging.dll,Microsoft.Practices.EnterpriseLibrary.Common.dll,Microsoft.Practices.ObjectBuilder.dll work only for Microsoft exchange server or for any type of servers like POP3,SMTP,IMAP etc. Kindly provide me the info. Thanks in advance.

cleverboy929
cleverboy929

Thank you. But the article is not clearly specifying that we can use the dll's with any type of servers.