As SharePoint developers we aren’t only responsible for creating the functionality business requires, but we are also responsible for the overall quality of the solution we build. This means an ergonomic fluent user interface, code that is suitable for changes and in my opinion one of the most important aspects, a good performance. Most of these topics are described in white papers, best practices and millions of books and blog posts… but wouldn’t it be a pleasure if it was already in the platform? Yes, indeed! That is why the SharePoint team provided us with the developer dashboard and the SPMonitoredScope class!
How to activate the developer dashboard?
There are several different ways to activate and deactivate the toolbar; it is up to your greater judgment to decide which one seems the most suitable for your situation. By default it is deactivated, so the first step would be the activation. You can do this by using STSADM, PowerShell or C#
stsadm -o setproperty -pn developer-dashboard -pv onstsadm -o setproperty -pn developer-dashboard -pv offstsadm -o setproperty -pn developer-dashboard -pv OnDemand
Add-PSSnapin Microsoft.SharePoint.Powershell$dash =[Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings;$dash.DisplayLevel = 'OnDemand';$dash.TraceEnabled = $true;$dash.Update()
How to provide custom traces in developer dashboard?
SPMonitoredScope inherits from IDisposable, so it is better to use it in combination with a using statement.
public class SPMonitoredScope : IDisposable
If you specify the onDemand setting, you receive a link button which displays a button which you can use to toggle the dashboard.
In order to demonstrate how easy it is, I created a new visual web part project and added this code in the page_load method. The code does nothing special, a single thread.sleep to simulate a long running operation and the monitoredscope class around it.
If we add the web part to the page and open the developer dashboard by using the new icon that appears next to your username, you can see that there is a new message in the dashboard and ULS view:
How to provide the developer dashboard on a custom master page?
In order to provide this functionality on your own master page or custom page, you need 2 components. The first component (SharePoint:DeveloperDashboardLauncher) is the link button that allows you to enable/disable the dashboard. The second component is the dashboard (SharePoint:DeveloperDashboard)control itself.
When should I use it?
I would recommend using the SPMonitoredScope as a best practice around each major code block in your public methods. And while testing your web part you can review (and maybe improve) the performance of your code in detail.
What about sandboxed solutions?
You cannot use SPMonitoredScope in Sandboxed Solutions.
Happy SPCoding !!