In this article we can explore a debugging scenario using 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 find out what is the underlying issue.
Solution
As the issue is happening in the production environment you can use the Diagnostics capabilities of SharePoint 2010.
Following are the activities involved in the solution:
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.
We can use the method WriteTrace() to write to ULS log. The advantage of writing to ULS log will be 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 Windows Event Log and thus requires more Permissions. The windows event log entries can be viewed through 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 inside 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 above code we have set Trace Severity to Verbose. The Verbose level is used to specify log information which contains more details.
Following are the members of Trace Severity enumeration.
Diagnostic Logging Configuration
You can configure the Log Levels in Central Administration. 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
On clicking the link you will get the page below.
For testing our web part please make the levels to Verbose as shown below.
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 write to the hard disk and eating a lot of processor cycles too. Thus verbose level logging is recommended only while debugging needs.
The Event Throttling settings control 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.
You can view the entry from the log file (latest log file) in 14 HIVE LOGS folder.
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
Summary
In this article we have explored the debugging capabilities of SharePoint 2010 through logging. Following are the point worth to note:
· SPDiagnosticsService class is used for Logging
· WriteEvent() writes to Windows Event Log and needs Security Privileges
· WriteTrace() writes to SharePoint Trace Log
· Central Administration can be used to configure Diagnostics Logging
· SPDiagnosticsServiceBase provides an alternative way of Logging
No comments:
Post a Comment