Introduction to the Logging Application Block

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

This topic includes a series of brief sections that provide information to help you decide whether the Logging Application Block is suitable for your requirements. This topic includes the following sections:

  • Common Scenarios
  • Example Application Code
  • When to Use the Logging Application Block

In addition to this introductory material, the documentation contains the following topics:

  • Developing Applications Using the Logging Application Block. This topic explains how to use the Logging Application Block in your applications. It shows how to configure the application block to perform common tasks and how to add application code to the application block where required.
  • Key Scenarios. This topic demonstrates how to use the application block to perform the most common logging operations.
  • Design of the Logging Application Block. This topic explains the decisions that went into designing the application block and the rationale behind those decisions.
  • Extending and Modifying the Logging Application Block. This topic explains how to extend the application block by creating your own custom trace listeners and how to modify the source code.
  • Deployment and Operations. This topic explains how to deploy and update the application block's assemblies and also contains information about the instrumentation in the block.
  • Logging QuickStart. This topic explains how to install and configure the QuickStart applications. This section contains a series of walkthroughs that demonstrate how to incorporate common logging operations in an application.

For details of the system requirements for the Caching Application Block, see System Requirements. For details of the dependencies for the Caching Application Block, see Application Block Dependencies.

Common Scenarios

Developers frequently write applications that require logging functionality. Typically, these applications format and log information in response to application events. For example, developers often write code to log information in response to unexpected conditions, such as an application exception, or failure to connect to a database. Developers also write code to trace application flow through components during the execution of an application use case or scenario.

Applications need to write information either locally or over the network. In some cases, you may have to collate events from multiple sources into a single location.

The Logging Application Block simplifies application development by providing a small set of easy-to-use classes and methods that encapsulate many of the most common logging scenarios. These scenarios include the following:

  • Populating and logging event information
  • Including context information in the event
  • Tracing application activities

Each task is handled in a consistent manner, abstracting the application code from the specific logging providers.

The Logging Application Block addresses the most common tasks that developers face when they write applications that require logging functionality. These tasks are arranged according to scenarios. Each scenario gives an example of a real-world situation, such as populating and raising events from code, discusses the particular requirements of the situation, and shows the code that accomplishes the task.

The goal of arranging these tasks according to scenarios is to provide context for the code. Instead of showing an isolated group of methods, with no sense of where they can best be used, the Logging Application Block uses scenarios to show code usage in situations that are familiar to many developers whose applications must use logging features.

The scenarios are the following:

  • Populating and raising events from code
  • Populating a log message with additional context information
  • Tracing activities and propagating context information
  • Checking filter status before constructing log messages.
  • Configuring options for trace listeners
  • Directing different event types to different trace listeners
  • Configuring log filters
  • Configure logging to be performed from a central location
  • Creating a custom trace listener

For details about each scenario, see Key Scenarios.

Example Application Code

The following code shows how to populate and raise an event in your application**.** The LogEntry object has a priority of 2 and belongs to both the Trace and UI Events categories.

LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = "Informational message";
logEntry.Categories.Add("Trace");
logEntry.Categories.Add("UI Events");

Logger.Write(logEntry);
'Usage
Dim logEntry As LogEntry = New LogEntry()
logEntry.EventId = 100
logEntry.Priority = 2
logEntry.Message = "Informational message"
logEntry.Categories.Add("Trace")
logEntry.Categories.Add("UI Events")

Logger.Write(logEntry)

When to Use the Logging Application Block

If your applications have a requirement to log information to Windows Event Log, e-mail, a database, a message queue, Windows Management Instrumentation (WMI), or a file, you should consider using the Logging Application Block to provide this functionality. In particular, the Logging Application Block is useful if you need to filter logging messages based on category or priority, if you need to format the messages, or if you need to change the destination of the message without changing the application code. The Logging Application Block is also designed to be extensible and includes the facility to create custom formatters and trace listeners, which you can adapt to meet your application's logging requirements.

Note

The Logging Application Block formatters do not encrypt logging information. Trace listener destinations receive logging information as clear text. This means that attackers that can access a trace listener destination can read the information. You can prevent unauthorized access to sensitive information. One approach is to use access control lists (ACLs) to restrict access to flat files. You can also create a custom formatter that encrypts log information. For information about how to create a custom formatter, see Extending the Logging Application Block.