Using SPMonitoredScope, Counters and SPCriticalTraceCounter

Something that I don’t always remember – you can use the SPMonitoredScope class to monitor your SharePoint code as it executes. The results then get sent to the Developer Dashboard and ULS logs. There’s a decent description of it on MSDN.

using (new SPMonitoredScope("My Monitored Scope"))
{
    // Do Stuff...
}

It’s worth noting that you can record more than just time (which I’d forgotten) – such as:

I must confess, I’m not entirely sure what these are for. I can’t ever seem to get them to show much information, and doesn’t the SPMonitoredScope return the execution time anyway?

Also, while not quite the same sort of counter, there is also the SPCriticalTraceCounter, that lets you record events…

SPCriticalTraceCounter.AddDataToScope(25, "My Event", 15, "Pop Goes the Weasel");

I’ve mentioned them (and the ‘level’ parameter) before.

These produce quite nice outputs:

Then you can also get Performance Counter information too.

SPHttpThrottleSettings throttleSettings =
      SPHttpThrottleSettings.GetHttpThrottleSettings(SPContext.Current.Site.WebApplication);
SPSystemPerformanceMonitorCollection monitors = throttleSettings.GenerateMonitors();
StringBuilder sb = new StringBuilder();
foreach (SPSystemPerformanceMonitor monitor in monitors)
{
	SPSystemPerformanceCounterMonitor counterMonitor = (SPSystemPerformanceCounterMonitor)monitor;
	sb.AppendFormat("{0} = {1}n", counterMonitor.Name, counterMonitor.Value.ToString());
}

For my system, that code output:

I’m not quite sure what else you might be able to get from that. I’m also guessing that you probably need fairly high permissions to see some of that information…

Advertisement
Using SPMonitoredScope, Counters and SPCriticalTraceCounter

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.