次の方法で共有


The DeveloperDashboardSettings console application

After the latest update to SP2010 the code for turning the developer dashboard on and off does not seem to work any more. Here are updated code for you. 

If you have not heard about it; The developer dashboard is a new feature in SP2010. It gives you a view on the bottom of each page. The view gives you the http request details, the time each took, web server details, database queries and more…

There are three settings for the dashboard; on, off and on-demand. The latest gives you an small button on the top right of the page, to toggle the setting yourself.

 

Since I will need this on a lot of different machines I made a small console app, with a parameter for the settings, making it easy to switch on and off.

 

Here are the complete code. Remember to add a reference to Microsoft.SharePoint.dll, set compilation to “any cpu” and .Net target framework to 3.5. Enjoy!

using System;
using Microsoft.SharePoint.Administration;

namespace SPDeveloperDashboard
{
    /// <summary>
    /// Enables or disables developer dashboard
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string arg;
                if (args.Length == 0)
                {
                    Usage();
                    Environment.Exit(0);
                }
                for (int i = 0; i < args.Length; i++)
                {
                    arg = args[i];
                    arg = arg.ToLower();

                    if (arg == "-help" || arg == "-h" || arg == "/?" || arg == "/h")
                    {
                        Usage();
                        Environment.Exit(0);
                    }
                    if (arg == "-on")
                    {
                        SPWebService cs = SPWebService.ContentService;
                        cs.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.On;
                        cs.DeveloperDashboardSettings.Update();
                        Console.WriteLine("Setting is turned on");
                    }
                    if (arg == "-off")
                    {
                        SPWebService cs = SPWebService.ContentService;
                        cs.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.Off;
                        cs.DeveloperDashboardSettings.Update();
                        Console.WriteLine("Setting is turned off");
                    }
                    if (arg == "-ondemand")
                    {
                        SPWebService cs = SPWebService.ContentService;
                        cs.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.OnDemand;
                        cs.DeveloperDashboardSettings.Update();
                        Console.WriteLine("Setting is on demand");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        static void Usage()
        {
            Console.WriteLine("Usage: SPDeveloperDashboard <-param> ");
            Console.WriteLine("  e.g. SPDeveloperDashboard -On");
            Console.WriteLine("  The -On Turns the SPDeveloperDashboard on");
            Console.WriteLine("  The -Off Turns the SPDeveloperDashboard off");
            Console.WriteLine("  The -OnDemand Sets the SPDeveloperDashBoard to OnDemand");
        }
    }
}

 

 

 

This code replaces ie:

SPPerformanceMonitor performanceMonitor = SPFarm.Local.PerformanceMonitor;
performanceMonitor.DeveloperDashboardLevel = SPPerformanceMonitoringLevel.On;