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";
oLogger.LogToOperations(message);

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

  • 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,
 EventSeverity.Error);
This will help the administrator to filter the log entries based on security level.

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

  • 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);

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

  • 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

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.