Compartir vía


Customizing the Logger in an Application

Typical Goals

In some circumstances, you may want to customize how the SharePoint Logger behaves in your production environment. For example, you might want to update the logger so that calls to LogToOperations copy messages to a third-party repository.

Solution

By default, the SharePointLogger class implements two key interfaces that define the logging and tracing functionality of the SharePoint Logger:

  • The IEventLogLogger interface defines logging functionality.
  • The ITraceLogger interface defines tracing functionality.

To customize the behavior of the SharePoint Logger, you can create your own implementations of these interfaces. You can then register your custom implementations with the SharePoint Service Locator, so any code that calls the SharePoint Logger automatically uses the updated functionality. In most cases, you should aim to register your custom implementations with the service locator at the same time as you deploy them to the SharePoint environment. A common approach is to use a feature receiver class to update the service locator type mappings.

This scenario focuses on how to register your implementations with the SharePoint Service Locator. For information about how to implement the IEventLogLogger interface and the ITraceLogger interface, see Creating Custom Logger Classes.

Updating Type Mappings for the Logger Interfaces

The following code shows how to register custom implementations of the IEventLogLogger interface and the ITraceLogger interface from within a feature receiver class. This example assumes that you have created classes named MyEventLogLogger and MyTraceLogger that implement the IEventLogLogger and ITraceLogger interfaces, respectively.

The example also assumes that you have added a reference to the Microsoft.Practices.SharePoint.Common.dll assembly, the Microsoft.Practices.ServiceLocation.dll assembly, and the Microsoft.SharePoint.dll assembly.

using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.ServiceLocation; 
using Microsoft.Practices.SharePoint.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.GetCurrent();
      
      IServiceLocatorConfig typeMappings =                                   
  serviceLocator.GetInstance<IServiceLocatorConfig>();

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