Wiring Your SharePoint Custom Applications to Log Errors

Developing custom web applications, workflows or web parts within SharePoint require some level of error handling to ensure smooth operation of your custom solution.

There are several techniques for doing this such as writing to a file or to the event log, but I came across a fantastic article a while back and just recently came around to trying it out. I’m happy to report that it works quite well.

The source of this information comes from the Blue Surf Tech blog titled The Sharepoint way for Application Error Alerts.

Now just to summarize the technique here:

1. Create a SharePoint custom list with custom columns based on the type of information you want to capture. For example you may want to capture the exception source, stack trace or inner exception to name a few. Or you may simply want to log informational messages that indicate areas of success in your code. The link above does a great job of describing the former, I’ll outline the latter in the next few steps.

2. The next step is to write the method that will log the information messages at specific intervals within your code to the custom SharePoint list you just built. Here is what my LogInfo method looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void LogInfo(string message, string sItemUrl, int sItemID, string sTitle)
{
    // create an handle to the current web context (works only in application pages)
    SPWeb web = SPContext.Current.Web;
 
    // create new item
    SPListItem item = web.Lists["Application Log"].Items.Add();
 
    item["Title"] = sTitle;
    item["Application"] = "SharePoint Employee List - Edit Form";
    item["Url"] = sItemUrl;
    item["ItemID"] = sItemID;
    item["Message"] = message;
    item["User"] = web.CurrentUser.Name;
 
    // save
    item.Update();
}

3. Finally, I just pick specific areas in my methods or workflow, for example an button submit event or a SendEmail method, to log the successes or any other type of message that might be helpful to track.

1
LogInfo("Successful item update.", listItem.Url, listItem.ID, listItem.Title);

There you have it, let me know how it works out. Feedback is most welcome.

Trackbacks/Pingbacks

  1. Links (4/12/2009) « Steve Pietrek - Everything SharePoint and Office - April 12, 2009

    […] Wiring Your SharePoint Custom Applications to Log Errors […]

Leave a Reply