Compartir a través de


How to: Provide a Custom Logger

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.

Overview

The Composite Application Library is designed to log messages throughout the library. To do this logging in a way that is not tied to a specific logging library, the Composite Application Library uses a logging façade, ILoggerFacade, to log its messages. This interface contains a single method named Log that logs messages. By default, the UnityBootstrapper sets a default TraceLogger as the designated logger, but this can easily be replaced for your application. This topic describes the general steps that you should follow to use a different logger in your application that uses the Composite Application Library.

For an example of how to provide a custom logger, see the Stock Trader Reference Implementation (Stock Trader RI). The Stock Trader RI uses the Enterprise Library Logging Application Block.

Prerequisites

This topic assumes that you already have a solution based on the Composite Application Library. For information about how to do this, see How to: Create a Solution Using the Composite Application Library.

Steps

The following procedure describes how to plug in a custom logger in a solution based on the Composite Application Library.

To plug-in a custom logger in an application that uses the Composite Application Library

  1. Make sure your project has a reference to the Microsoft.Practices.Composite assembly.

  2. Create a logger class (for example, CustomLogger) that implements the ILoggerFacade interface, as shown here.

    // The following using statement is required.
    using Microsoft.Practices.Composite.Logging;
    
    ...
    
    public class CustomLogger : ILoggerFacade
    {
    
    }
    
  3. Implement the Log method in the logger class as prescribed by the **ILoggerFacade **interface. This method takes three parameters:

    • message. This is the message to be logged.
    • category. This is the category of the event to be logged. The valid options are Debug, Exception, Info, and Warn.
    • priority. This is the priority of the event to be logged. The valid options are None, High, Medium, and Low.

    In this method, you should log the event. You can write your own code to do this or use a logging framework. The following code, taken from the EntlibLoggerAdapter class included in the Stock Trader RI shows how to implement the Log method using the Enterprise Library Logging Application Block.

    public void Log(string message, Category category, Priority priority)
    {
        // Logger is a static class provided by the Enterprise Library
        // Logging Application Block.
        Logger.Write(message, category.ToString(), (int)priority);
    }
    
  4. In your application's bootstrapper, override the LoggerFacade property and return a new instance of your custom logger** **class created in step 2, as shown here.

    using Microsoft.Practices.Composite.Logging;
    
    ...
    
    // CustomLogger is a custom logger implementation.
    CustomLogger logger = new CustomLogger();
    
    protected override ILoggerFacade LoggerFacade
    {
         get
         {
              return logger;
         }
    }
    

Outcome

You will have a custom logger configured in your application. This logger will be used in your application based on the Composite Application Library.

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.