Debug Web Parts

This topic is close particularly close to my heart, since my job is filled with debugging web parts. The first thing you should note is that in ASP.NET, there’s a customErrors section in the web.config files. However hard you set it to false in every imaginable level SharePoint will override it, and shows its useless “Oops, exception, I won’t tell you anything about it” form. Unless you set callStack attribute of the SharePoint element in the very same web.config file to true.

But of course serving error messages with call stack is not the most user friendly way of showing that something went wrong. SharePoint provides a nice logging infrastructure. The key player is the ULS (Unified Logging System) log. You can find it in the 14/ folder of your SharePoint installation, under the log folder. Although you’ll be much better using the free ULS Viewer tool.

SharePoint logs a great deal of information to this log by default. For example, unhandled exceptions which bubble up to the UI are all logged here. But you can use this log programmatically, too. You can use the SPDiagnosticsService class to write your events to the ULS logs, or roll out your custom logging implementation by subclassing the SPDiagnosticsServiceBase class.

SharePoint 2010 has a component called the Developer Dashboard which is very similar to ASP.NET tracing. By default, it’s turned off, so you have to use stsadm or PowerShell to make it appear. There are two modes of operation: On displays the dashboard icon on every page, so anyone with rights to the page can view its output on every page. There’s another mode called OnDemand, which displays an icon on every page, on which you can access the dashboard information. By default the dashboard records a variety of instrumental information, but of course you can add your own informations to it. You have to wrap your all your custom components you’d like to display in SPMonitoredScopes. Here’s a quick code:

using (var scope = new SPMonitoredScope(“My long running method”))
{
//Your code here.
}

Now run time information will be available on the dashboard for you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s