Export (0) Print
Expand All

How to: Provide a Custom Logger

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 TextLogger 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 TextLogger class included in the Composite Application Library shows how to implement the Log method.

    public void Log(string message, Category category, Priority priority)
    {
        string messageToLog = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{1}: {2}. Priority: {3}. Timestamp:{0:u}.", DateTime.Now,        category.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture), message, priority.ToString());
    
        Console.WriteLine(messageToLog);
    }
    
    Ff650626.note(en-us,PandP.10).gifNote:
    The hardcoded string in the preceding code snippet is used only to provide a working code sample and is not a best practice. For example, it would be a better practice to store the string in a resources file, as shown in the default logger (TextLogger) provided by the Composite Application Library.

  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.

More Information

For more information about extensibility points in the Composite Application Library, see Customizing the Composite Application Library.

For information about how to create custom region adapters, see How to: Create a Custom Region Adapter.

For a complete list of How-to topics included with the Composite Application Guidance, see Development Activities.


Home page on MSDN | Community site

Show:
© 2014 Microsoft