How to implement logging in SharePoint 2010–Part 3

In this post, I’d be focusing on how to create custom log entries using the SharePoint Logger (ILogger) component.

The LogToOperations methods and its overload can be used to create the custom log entries to Windows event log and the SharePoint 2010 ULS log.

  • It is recommended that enough information is provided to IT professionals to remediate the problem, when an event is logged using SharePoint Logger component

  • The default implementation of the LogToOperations method will write the event to the Windows event log and the ULS log.

  • Do not log the events to the  event source with limited information, like only the event message below :-

IServiceLocator oServiceLocator = SharePointServiceLocator.GetCurrent();
ILogger oLogger = oServiceLocator.GetInstance<ILogger>();

// Log an event with a message.
string message = "The current user does not have enough permissions for this operation";
  • It is recommended to provide  comprehensive information like Event Id, Error Message and Event Severity while logging.

// Log an event with a message and a severity level.
oLogger.LogToOperations(message, EventSeverity.Error);

// Log an event with a message, an event ID, and a severity level.
oLogger.LogToOperations(message, (int) EventLogEventId.MissingID,
This will help the administrator to filter the log entries based on security level.
  • For Sandbox solutions the event severity enumeration of SandboxEventSecurity should be used.

  • Do not use EventSeverity enumeration for Sandbox solutions, it will throw an error

  • It is also good to have the Event Category logged to the event source

// Log an event with a message, an event ID, a severity level, and a category.
string oArea = "New Custom Area"
string oCategory = "Operations";
string oAreaCategory = string.Format("{0}/{1}", oArea, oCategory);
logger.LogToOperations(msg, (int) EventLogEventId.MissingID,
                       EventSeverity.Error, oAreaCategory);
  • Do not leave the DiagnosticsArea and DiagnosticsCategory as unspecified.

  • Because the SharePointLogger will assign a default value of ‘Patterns and Practices’ to the area value and the default value of ‘SharePoint Guidance’ to the Category.

  • The exception messages can also be directly passed as the parameter to the LogToOperations method. This should be avoided as much as possible


 Subscribe to my blog

Leave a Reply