SP 2010: Developing for performance Part 4 – Logging

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

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 this article series

Part 1 – SP 2010: Developing for performance Part 1 – Developer Dashboard
Part 2 – SP 2010: Developing for performance Part 2 – SPMonitoredScope
Part 3 – SP 2010: Developing for performance Part 3 – Caching in SharePoint 2010
Part 4 – SP 2010: Developing for performance Part 4 – Logging
Part 5 – SP 2010: Developing for performance Part 5 – Disposal patterns and tools
Part 6 – SP 2010: Developing for performance Part 6 – CSS Sprites
Part 7 – SP 2010: Developing for performance Part 7 – Crunching those scripts
Part 8 – SP 2010: Developing for performance Part 8 – Control that ViewState

Part 4 (this article):
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.

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)

image

image

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.

image

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

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.

You may also like...

  • Pingback: Error Logging in SharePoint « SPMatt

  • Someone

    Some pictures are broken.

    • http://www.zimmergren.net/ Tobias Zimmergren

      Hi, the images are working properly again – they went missing in the migration to the new blog.
      Enjoy.
      Tobias.

  • http://agrahoteldeals.com/ Agra Hotel Deals

    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.

  • http://www.assospark.com/ assos otelleri

    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.

  • http://examsalerts.in/ Ranjeet

    Linkbuilding packages some times we ignore this sort of things & also suffer a lot as well.

  • http://casinospiele2000.com/ casino spiele

    Thank you for taking time to share it with the readers, I am more than happy to have come across it. Keep up the good work.

  • http://www.online-casino-berater.com/ online casino

    I have been reading and reviewing your article for a few minutes to ponder some of your sound points. I like the way you think and I agree on a large portion of this content.

  • http://pulse.yahoo.com/_IAGCEJU3NF6CBU54J5DXF5V3PU Baboos

    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?

    • http://www.zimmergren.net/ Tobias Zimmergren

      Hi Baboos,

      You can take a look at this post: http://weblogs.asp.net/gunnarpeipman/archive/2009/02/25/sharepoint-writing-messages-to-uls-unified-logging-system.aspx 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.

  • rony

    Glad to visit your blog, I look forward to more reliable articles and I think we all like to thank so many fine articles

    • http://www.zimmergren.net/ Tobias Zimmergren

      Thank you Rony.

  • johnakp

    I really loved reading your blog. It was very well authored and easy to undertand.

    online casino kostenlos | mobiles casino

  • Pingback: Tools for your SharePoint 2013 development toolbox | Tobias Zimmergren's thoughts on development