Partager via


Creating a Custom Logger

Retired Content

This content and the technology described is outdated and is no longer being maintained. For more information, see Transient Fault Handling.

patterns & practices Developer Center

The Autoscaling Application Block includes two logging implementations that you can select from in the block configuration: a logger that uses the System.Diagnostics namespace and a logger that uses the Enterprise Library Logging Block.

You can also create your own custom logger; for example, to integrate with a third-party logging tool. A custom logger implementation must implement the ILogger interface shown in the following code sample:

public interface ILogger : IDisposable
{
    void Write(IEnumerable<string> sources, TraceEventType eventType, 
               int eventId, string message, IDictionary<string, object> data);
}

If you want to pass custom configuration parameters to your custom logger, your custom logger class should have a constructor that takes a single parameter of type NameValueCollection, as shown in the following code sample. Note the use of the ConfigurationElementType attribute to decorate the class.

[ConfigurationElementType(typeof(CustomLoggerData))]
public class CustomLogger : ILogger
{
    public CustomLogger(NameValueCollection attributes)
    {
        ...
    }

    public void Dispose()
    {
        ...
    }

    public void Write(IEnumerable<string> sources, TraceEventType eventType, 
                      int eventId, string message, IDictionary<string, object> data)
    {
        ...
    }
}

You must deploy the assembly that implements your custom logger with the Autoscaling Application Block.

You must tell the Autoscaling Application Block about your custom logger by using the Enterprise Library configuration tool. The following procedure shows how to configure the block to use a custom logger.

Configuring the Autoscaling Application Block to use a custom logger

  1. To change the logger implementation that the block uses, click the plus sign icon at the top right of the Logger panel and then click Set Logger. You can choose to use the Enterprise Library Logging Block or a custom logger implementation.

    Hh680926.DBC488616FDFDBD87920026FAD06CAB6(en-us,PandP.50).png

  2. To use a custom logger, click Use CustomLoggerData, and then click Yes to confirm the change. Use the Type Name box to identify the type of your custom logger implementation.

    Hh680926.7DDDF727C3FC16B8F876A5223068B4D3(en-us,PandP.50).png

  3. You can provide any additional configuration data that your custom service information store requires by adding attributes. Each attribute is a key/value pair. The block passes all the key/value pairs to the constructor of your custom service information store class.

    Hh680926.935F4A5EB181F0064984DD10DA2ECBDB(en-us,PandP.50).png

Next Topic | Previous Topic | Home

Last built: June 7, 2012