Export (0) Print
Expand All

Extending 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.

The Logging Application Block is designed to suit a variety of applications and to provide the most commonly used logging functions. You can extend the application block through designated extension points. Typically, these are custom classes, written by you, that implement a particular interface or derive from an abstract class. Because these custom classes exist in your application space, you do not need to modify or rebuild the application block. Instead, you designate your extensions using configuration settings. Additionally, with extension points, you can adapt the application block to suit the needs of any particular application. You can extend the capabilities of the application block by adding custom trace listeners, filters, and formatters.

The following procedure describes the general approach to extend the Logging Application Block.

To extend the Logging Application Block

  1. Create a new custom class and add it to your project.
  2. Make sure that the class implements the required interfaces, constructors, and methods.
  3. Add the custom object to the configuration of the Logging Application Block using the Enterprise Library configuration tools:
    • Specify your custom class as the type name.
    • Specify any custom configuration properties by modifying the attributes of the object.

To create a custom formatter

  1. Create a new class, and then add it to your project.
  2. (Optional) To use elements without fully qualifying each one, you can add the following using statement (C#) or Imports statement (Visual Basic) to the top of your source code file.
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
    For Visual Basic projects, you can also use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer, and then click Properties on the Project menu. When the Project Designer appears, click the References tab.

  3. Specify that the class implements ILogFormatter.
  4. Add the class attribute ConfigurationElementType. Specify the type CustomFormatterData as the attribute parameter.
    public class MyFormatter : ILogFormatter
  5. Add a constructor that has parameter of type NameValueCollection.
    public MyFormatter (NameValueCollection attributes)
  6. Add the Format method to your class, and then implement the required behavior.
    public string Format(LogEntry log)

For more details about how to extend the Logging Application Block, with an example that shows how to create a custom event sink, see Walkthrough: Creating a Custom Trace Listener.

© 2015 Microsoft