Creating Custom Logger Classes

In continuation to my previous posts
SharePoint logger part-1
SharePoint logger part-2
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.
The SharePointLogger class implements two key interfaces named IEventLogLogger and ITraceLogger that define how events and traces are logged. You can change the behavior of the SharePoint Logger by providing alternative implementations of these interfaces:
  • To change the behavior of the ILogger.LogToOperations method, create a class that implements the IEventLogLogger interface.
  • To change the behavior of the ILogger.TraceToDeveloper method, create a class that implements the ITraceLogger interface.
For example, you might want to customize the SharePoint Logger so that the LogToOperations method writes a message to a database instead of to the Windows event log. Alternatively, you might want to modify the behavior of the TraceToDeveloper method, so that trace messages are written to a dedicated location instead of to the ULS trace logs that also contain many other SharePoint-related trace messages.
The following code example shows how you can override the IEventLogLogger interface to provide your own event logger implementation. Notice that the interface requires you to implement a single method named Log.
public class MyEventLogLogger : IEventLogLogger

  public void Log(string message, int eventId, EventSeverity severity,
                  string category)
     // Custom code to handle event logging request…
The following code shows how you can override the ITraceLogger interface to provide your own trace logger implementation. This interface defines a single method named Trace.
public class MyTraceLogger : ITraceLogger
  public void Trace(string message, int eventId, TraceSeverity severity,
                    string category)
     // Custom code to handle tracing request…
After you develop and deploy your custom logging and tracing classes, you must register these classes with the SharePoint Service Locator as implementations of IEventLogLogger and ITraceLogger respectively. Typically, you should use a feature receiver class to register your implementations at the point of deployment.

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;

public class MyFeatureReceiver : SPFeatureReceiver
   // ...

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

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


Popular Posts