Share via


Customizing the Logger in an Application

Typical Goals

In this scenario, you want to configure your application to use a custom logging component. This scenario applies to deployed applications.

Solution

In Visual Studio, add a reference to the SharePoint Guidance Library, Microsoft.Practices.SPG.Common.dll, and to Microsoft.Practices.ServiceLocation.dll. Create a new class that implements the IEventLogLogger or ITraceLogger interface. For example, your new class might encapsulate an existing logging mechanism that you want to integrate into your application. Use the SharePoint service locator to get a reference to an IServiceLocatorConfig interface. Use the RegisterTypeMapping method of the IServiceLocatorConfig object to configure the service locator with type mappings to your new logging classes. For information about adding the service locator, see Getting Services from the SharePoint Service Locator.

Customizing the Logger in an Application

The following code demonstrates how to customize the logger.

using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SPG.Common.ServiceLocation; 
using Microsoft.Practices.SPG.Common.Logging;

[CLSCompliant(false)]
[Guid("8b0f085e-72a0-4d9f-ac74-0038dc0f6dd5")]
public class MyFeatureReceiver : SPFeatureReceiver
{ 
    /// ...

    [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    {
       IServiceLocator serviceLocator = SharePointServiceLocator.Current;
       IServiceLocatorConfig typeMappings =  
                                 serviceLocator.GetInstance<IServiceLocatorConfig>();

       typeMappings.RegisterTypeMapping<IEventLogLogger, MyEventLogLogger>();
       typeMappings.RegisterTypeMapping<ITraceLogger, MyTraceLogger>();
    }   
}

This code establishes service locator type mappings for the IEventLogLogger interface and the ITraceLogger interface. The implementation class MyEventLogLogger is mapped to the IEventLogLogger interface. The implementation class MyTraceLogger is mapped to the ITraceLogger interface. These mappings mean that the output for any objects that use the SharePoint logger use these logging implementations. Notice that you did not need to alter any code in the component that is being tested.

Usage Notes

To implement this scenario, you must provide event log logger and trace logger implementations. The following code shows examples of these implementations.

public class MyTraceLogger : ITraceLogger
{
  [SharePointPermissionAttribute(SecurityAction.InheritanceDemand, 
                                                                ObjectModel = true)]
  [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  public void Trace(string message, int eventId, TraceSeverity severity, 
                                                                    string category)
  {
     /// ... custom code to handle tracing request ...
  }
}

public class MyEventLogLogger : IEventLogLogger
{
  [SharePointPermissionAttribute(SecurityAction.InheritanceDemand, 
                                                                ObjectModel = true)]
  [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  public void Log(string message, int eventId, EventLogEntryType severity, 
                                                                     string category)
  {
     /// ... custom code to handle event logging request ...
  }
}

Home page on MSDN | Community site