Compartir vía


Creating Custom Logger Classes

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. For more information, see Customizing the Logger in an Application.

For more information about the ITraceLogger interface and the IEventLogger interface, see Design of the SharePoint Logger.