In my previous post about the Developer Dashboard I showed you how to switch it on/off/on demand by PowerShell.
Ofcourse this can be done in Visual Studio as well:
SPWebService cs = SPWebService.ContentService; cs.DeveloperDashboardSettings.DisplayLevel = (SPDeveloperDashboardLevel)selectedValue; cs.DeveloperDashboardSettings.Update();
Make sure, if you make a webpart to set the developer dashboard level, you deploy this as a Farm Solution and deploy it to the Central Administration (Project Properties, set Site URL to your CA location).
The code will not work on another site, only at the CA, because the developer dashboard is a farm wide setting. If you do try to use the webpart in another site than CA, a security exception will be thrown:
System.Security.SecurityException: Access denied.
When you deploy the solution to the CA and try to use it on another site (or visa versa) you get the message:
A Web Part or Web Form Control on this Page cannot be displayed or imported. The type is not registered as safe.
After deploying a webpart to set the level of the Developer Dashboard, just go to the Central Administration e.g. to the Application Management page and edit the page. Two webpart zones will be visible and just add the webpart as you normally would.