Reader Level:
ARTICLE

Debugging Using SPDiagnosticsService in Sharepoint 2010

Posted by Jean Paul Articles | SharePoint September 29, 2012
In this article we can explore a debugging scenario using the SPDiagnosticsService class and the SharePoint 2010 Logging Severity Levels.
  • 0
  • 0
  • 8285
 

In this article we can explore a debugging scenario using the SPDiagnosticsService class and the SharePoint 2010 Logging Severity Levels.

Scenario

Your customer reported that in the middle of an operation the SharePoint site is not responding. You need to determine what the underlying issue is.

Solution

Since the issue is happening in the production environment you can use the Diagnostics capabilities of SharePoint 2010.

The following are the activities involved in the analysis:

  1. Write events to the Log
  2. Change the SharePoint Log Level
  3. Deploy the Solution
  4. Repeat the User Activity
  5. Trace the problem

Here we are going to perform the first 3 activities.

SPDiagnosticsService

The class provides a Diagnostics service manager for SharePoint. The class is residing in namespace Microsoft.SharePoint.Administration.

We can use the static property named Local to get an instance of the service class; see:

SPDiagnosticsService.jpg

We can use the method WriteTrace() to write to the ULS log. The advantage of writing to the ULS log will be the following:

  • Share the common location of SharePoint log
  • Combined analysis of application trace along with other service application traces

Note: Please note that the WriteEvent() method writes to the Windows Event Log and thus requires more Permissions. The windows event log entries can be viewed through the Windows Event Viewer.

Example

Create a new SharePoint solution and add a Web Part into it. Add a button on the web part and add the following code in the button click event.

protected void Button1_Click(object sender, EventArgs e)

{

    SPDiagnosticsCategory category = new SPDiagnosticsCategory(

"My Application Category",

TraceSeverity.Verbose,

EventSeverity.Verbose

);

 

    SPDiagnosticsService.Local.WriteTrace(

0,

category,

TraceSeverity.Verbose,

"Log information"

);

}

In the preceding code we have set the Trace Severity to Verbose. The Verbose level is used to specify log information which contains more details.

The following are the members of the Trace Severity enumeration.

members-of-Trace-Severity-enumeration.jpg

Diagnostic Logging Configuration

You can configure the Log Levels in Central Administration. The following are the properties of diagnostics logging which are configurable:

  1. Log file location

  2. Number of Days to retain Log file

  3. Least critical event level

  4. Least trace event level

  5. Enable Service Application Logs

To configure these open Central Administration > Monitoring > Configure diagnostics logging; see:

Diagnostic-Logging-Configuration.jpg

On clicking the link you will get the following page:

Configuration-Diagnostic-Logging.jpg

For testing our web part please make the levels to Verbose as shown below:

testing-our-web-part.jpg

Click the OK button to save the changes.

Severity Levels

The Severity Levels are used to specify the log information level. The Severity Level is used to process or discard log information by SharePoint.

A Verbose level logging involves more writes to the hard disk and eating a lot of processor cycles too. Thus verbose level logging is recommended only for debugging purposes.

The Event Throttling settings controls the severity of events captured in the Windows event log and the trace logs. As the severity decreases the number of events logged will increase.

Testing the Application

We can now test our web part. Build, Deploy and Insert the web part into a page. Click the button to create the trace log entry.

Testing-the-Application.jpg

You can view the entry from the log file (latest log file) in the 14 HIVE LOGS folder.

HIVE-LOGS-folder.jpg

This concludes our test with Diagnostics Logging.

SPDiagnosticsServiceBase

SPDiagnosticsServiceBase is an abstract class. This class can be used to create a concrete class which can be used for writing custom diagnostics categories. Inside our implementation class the Category can be explicitly provided.

Additionally you can specify the Product Name for the log entry while implementing the concrete class.  

References

http://tinyurl.com/sp2010-diag
http://tinyurl.com/sp2010-spdiag
http://tinyurl.com/sp2010-spdiagbase

Summary

In this article we have explored the debugging capabilities of SharePoint 2010 through logging. The following are the points worth noting:

  • The SPDiagnosticsService class is used for Logging

  • WriteEvent() writes to the Windows Event Log and needs Security Privileges

  • WriteTrace() writes to the SharePoint Trace Log

  • Central Administration can be used to configure Diagnostics Logging

  • SPDiagnosticsServiceBase provides an alternative way of Logging

COMMENT USING

Trending up