SharePoint 2010 developing for performance article series:
In this series of articles I will briefly introduce you to some key concepts when it comes to developing for performance in our SharePoint 2010 applications.
Related articles in series
- SP 2010: Developing for performance Part 1 - Developer Dashboard
- SP 2010: Developing for performance Part 2 - SPMonitoredScope
- SP 2010: Developing for performance Part 3 - Caching in SharePoint 2010
- SP 2010: Developing for performance Part 4 - Logging
- SP 2010: Developing for performance Part 5 - Disposal patterns and tools
- SP 2010: Developing for performance Part 6 - CSS Sprites
- SP 2010: Developing for performance part 7 - Crunching those scripts
- SP 2010: Developing for performance Part 8 - Control that ViewState
This is part 4.
In SharePoint 2010 (well, 2007 too for that matter) you need to think about proper logging in your applications to ensure that any problems, issues or other events are lifted to the ULS Logs (Unified Logging System) – that way the administrators can easily view the logs and track down problems with your applications. In this article I will talk a bit about how you can utilize the logging capabilities in SharePoint 2010.
ULS Logs
The ULS Logs are the main place for SharePoint to output it’s diagnostics information. We will take a quick look at how we can read the logs and obviously how we can write custom logging messages to the logs.
Reading the ULS Logs in SharePoint 2010
In order to read the ULS Logs you’ll need access to the SharePointRoot (14LOGS) folder. But to make the life even easier for us Microsoft released a tool called the ULS Viewer which you can download here: http://code.msdn.microsoft.com/ULSViewer
With this tool you can quite easily read through the logs in SharePoint 2010 without any issues.
There’s plenty of resources on the web about how to use the ULS Viewer, so go take a look at any one of them for some usage-instructions.
Read more: Download (docx): ULS Viewer documentation
Writing to the ULS Logs from you SharePoint 2010 application
The other side of the logs are of course writing to the logs. This is not a very hard task to do in SharePoint 2010, and I’ll outline the basic steps to do so here.
Normally I create a new class or at least a method to take care of the logging, and it can look like this:
public static void WriteTrace(Exception ex)
{
SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
diagSvc.WriteTrace(0,
new SPDiagnosticsCategory("TOZIT Exception",
TraceSeverity.Monitorable,
EventSeverity.Error),
TraceSeverity.Monitorable,
"An exception occurred: {0}",
new object[] {ex});
}
You can use the aforementioned code by calling the method like so:
try
{
throw new Exception("Oh no, application malfunctioned! UnAwesomeness!!!");
}
catch(Exception ex)
{
WriteEvent(ex);
}
It’s not very hard at all – once you’ve done that, you’re all set to kick off your custom applications and just call your custom logging method. Obviously you should create a proper logging class to take care of all your logging in your applications.
Results
Using the ULS Viewer you can easily track down the error messages by filtering on your category (in my case it’s TOZIT Exception)
![]()
![]()
Event Logs
Even though the ULS Logs takes care of most of the diagnostics logging today, it might be reasonable to output information into the Event Logs sometime.
Writing to the Event Logs from you SharePoint 2010 application
Just as when you’re doing ULS Logging, you can do logging to the Event Logs. It’s just as simple, but you replace the method “WriteTrace” with “WriteEvent” like this:
public static void WriteEvent(Exception ex)
{
SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
diagSvc.WriteEvent(0,
new SPDiagnosticsCategory("TOZIT Exception",
TraceSeverity.Monitorable,
EventSeverity.Warning),
EventSeverity.Error,
"Exception occured {0}", new object[] {ex});
}
Results
You can view the logs in the Event Logs on your SharePoint server as you would read any other logs.
![]()
Can I do more?
There’s plenty of cool things to do with the logging mechanism in SharePoint 2010, so you should definitively get your hands dirty playing around with it.
If you for example want to tag the log entries with your company name, clients name or project name – you can easily change that behavior as well. Take a look at my friend Waldek’s post about it here: http://blog.mastykarz.nl/logging-uls-sharepoint-2010/
Related resources
- Find error messages with Correlation ID token in SP 2010
- MSDN: Debugging and Logging capabilities in SharePoint 2010
- TechNet: Configure diagnostics logging in SharePoint 2010
Summary
As a part of the article series focusing on some general guidelines for performance in your applications, logging is a major player. If you master your logs in terms of monitoring and custom application logging you will quickly come to realize how valuable it is.
This is intended as a starting point for you to get familiar with the logging-capabilities in SharePoint 2010.
Comments are closed
Archived comments
Some pictures are broken.
Hi, the images are working properly again - they went missing in the migration to the new blog.
Enjoy.
Tobias.
I just wanna thank you for sharing your information and your site or blog this is simple but nice article I have ever seen i like it i learn something today.
I am trying to implement a common logging solution that should work both on SharePoint 2010 and 2007 . New cool logging features will not work with MOSS. Any thoughts?
Hi Baboos,
You can take a look at this post: http://weblogs.asp.net/gunn... which describes how to create such a logging mechanism. There are ways in SharePoint 2007 to push info to the ULS logs, just be sure to know what the consequences are before you do. Be sure to read that blog post and you should be on your way.
To be honest though, I would have created one Logging solution for 2007 and one for 2010, since the SharePoint 2010 logging framework is more capable and is open for developers to utilize safely.
Regards,
Tobias.
Glad to visit your blog, I look forward to more reliable articles and I think we all like to thank so many fine articles
Thank you Rony.