Set Usage Analysis

Posted on Posted in SharePoint 2007, STSADM Commands

As part of our upgrade I wanted to make sure that usage analysis was fully enabled and configured so that site collection administrators could track how their sites were being used. As there was no built-in stsadm command to do this I had to create my own: gl-setusageanalysis. Turns out that setting the usage settings via code is simple until you get to the point where you want to configure the advanced usage analysis and logging which is an SSP level configuration. With most SSP configurations this one has no public interface – fortunately it’s reasonably easy to use reflection to get at the code to make the change (a very simple method call – it’s beyond me why this isn’t made public – I’m starting to sound like a broken record but it’s extremely frustrating). The code is shown below:

   1: public override int Run(string command, StringDictionary keyValues, out string output)
   2: {
   3:  output = string.Empty;
   5:  InitParameters(keyValues);
   7:  // Get the usage settings from the administration web service (basically the Central Administration web application)
   8:  SPUsageSettings usageSettings = SPWebService.AdministrationService.UsageSettings;
  10:  if (Params["enablelogging"].UserTypedIn)
  11:   usageSettings.LoggingEnabled = bool.Parse(Params["enablelogging"].Value);
  13:  if (Params["enableusageprocessing"].UserTypedIn)
  14:   usageSettings.UsageProcessingEnabled = bool.Parse(Params["enableusageprocessing"].Value);
  16:  if (Params["logfilelocation"].UserTypedIn)
  17:   usageSettings.LogFilesDirectory = Params["logfilelocation"].Value;
  19:  if (Params["numberoflogfiles"].UserTypedIn)
  20:   usageSettings.NumberLogFiles = uint.Parse(Params["numberoflogfiles"].Value);
  22:  if (Params["processingstarttime"].UserTypedIn) // Validation makes sure that end time is also provided
  23:  {
  24:   DateTime startTime = DateTime.Parse(Params["processingstarttime"].Value);
  25:   DateTime endTime = DateTime.Parse(Params["processingendtime"].Value);
  26:   usageSettings.SetProcessingInterval(startTime.Hour, startTime.Minute, endTime.Hour, endTime.Minute);
  27:  }
  28:  usageSettings.Update();
  31:  if (Params["sspname"].UserTypedIn)
  32:  {
  33:   string sspname = Params["sspname"].Value;
  35:   // The SSP is locked down so we need to use reflection to get at it.
  36:   object sharedResourceProvider = Utilities.GetSharedResourceProvider(sspname);
  38:   if (Params["enableadvancedprocessing"].UserTypedIn)
  39:   {
  40:    Type configurationType = Type.GetType(
  41:      "Microsoft.SharePoint.Portal.Analytics.Configuration, Microsoft.SharePoint.Portal, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
  43:    bool enable = bool.Parse(Params["enableadvancedprocessing"].Value);
  44:    //Microsoft.SharePoint.Portal.Analytics.Configuration.SetAnalyticsEnabledOnSrp(sharedResourceProvider, enable);
  45:    Utilities.ExecuteMethod(configurationType, "SetAnalyticsEnabledOnSrp",
  46:          new Type[] {sharedResourceProvider.GetType(), typeof (bool)},
  47:          new object[] {sharedResourceProvider, enable});
  48:   }
  50:   if (Params["enablequerylogging"].UserTypedIn)
  51:   {
  52:    Type queryLoggerType = Type.GetType(
  53:      "Microsoft.Office.Server.Search.Query.QueryLogger, Microsoft.Office.Server.Search, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
  55:    bool enable = bool.Parse(Params["enablequerylogging"].Value);
  56:    //Microsoft.Office.Server.Search.Query.QueryLogger.SetQueryLoggingEnabledOnSsp(sharedResourceProvider, enable);
  57:    Utilities.ExecuteMethod(queryLoggerType, "SetQueryLoggingEnabledOnSsp",
  58:          new Type[] {sharedResourceProvider.GetType(), typeof (bool)},
  59:          new object[] {sharedResourceProvider, enable});
  60:   }
  62:   Console.WriteLine("The settings updated may require an iisreset before the changes are visible.");
  63:  }
  65:  return 1;
  66: }

The syntax of the command can be seen below:

C:\>stsadm -help gl-setusageanalysis

stsadm -o gl-setusageanalysis

Sets the usage analysis settings.

        [-enablelogging <true | false>]
        [-enableusageprocessing <true | false>]
        [-logfilelocation <log file location>]
        [-numberoflogfiles <number of log files to create (0-30)>]
        [-processingstarttime <usage analysis start time>]
        [-processingendtime <usage analysis end time>]
        [-sspname <SSP name>]
        [-enableadvancedprocessing <true | false>]
        [-enablequerylogging <true | false>]

Here’s an example of how to enable usage analysis:

stsadm -o gl-setusageanalysis -enablelogging true -enableusageprocessing true -logfilelocation "c:\Logs" -numberoflogfiles 30 -processingstarttime "10:00PM" -processingendtime "1:00AM" -sspname SSP1 -enableadvancedprocessing true -enablequerylogging true

If you’ve changed any of the SSP related parameters then you may need to do an IIS reset before the changes show up in the browser.

7 thoughts on “Set Usage Analysis

  1. I Have been trying to get your usage anaysis class working but when I recompile I get an error that Utilities.ExecuteMethod and Utilities.GetSharedResourceProvider do not exist. Any help would be greatly appreciated. I have used your comamnds to help automate my install and would really like to get this one working. Thanks for the great posts!

  2. Make sure that you have the latest download. Those two methods are utility methods that I created to help reduce the amount of reflection work I had to do – they should exist in the Utilities.cs class and I can’t imagine why they wouldn’t be there if you’ve got a complete download.

  3. Gary,
    I’m in an environment where sadly we are unable to install your stsadm extensions. I am working to script my installation and configuration of MOSS and wanted to be able to set up Usage Analysis Processing along the lines of what you’ve done with your extensions. Some of the properties that your extension uses are supported in vanilla stsadm, i.e. loggingenabled and usageprocessingenabled, but I guess others are not? I tried setting the property job-usage-analysis and whilst I didn’t get any error I also didn’t get my job configured to run between the times I specified. I’m not a developer (though I have in the past), but I have seen mention of Reflection. Would Reflection allow me to determine what the properties are for stsadm that I can and cannot configure through vanilla stsadm?
    Thanks, Des

Leave a Reply

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