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 =
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…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>