Remotely enabling windows azure diagnostics

I frequently need to Enable and configure collecting performance counters remotely in my windows azure deployment,  There are a few tasks you are trying to accomplish

1- Get all roles you have for your deployment

2- Get the Diagnostics manager associated with each instance of each role

3- Set your Windows Azure Diagnostics Settings. 

I created the following helper class to make it easier for me, hope you find it useful:)

    1:          #region Default Counters
    2:          static string[] defaultCounterNames = new string[] 
    3:          {
    4:              @"\Memory\Available Mbytes", 
    5:              @"\Processor(_Total)\% Processor Time", 
    6:              @"\Processor(*)\% Processor Time",
    7:              @"\Process(*)\Working Set",
    8:              @"\Process(*)\Private Bytes",
    9:              @"\Process(*)\Working Set Peak",
   10:              @"\Process(*)\Virtual Bytes",
   11:              @"\Process(*)\Virtual Bytes Peak"
   12:          };
   13:          #endregion
   14:   
   15:          public static void SetState(string connectionString, string deploymentId , int sampleRateInSeconds, int transferPeriodInSeconds, string[] counterNames = null)
   16:          {
   17:              if (counterNames == null || counterNames.Length == 0)
   18:              {
   19:                  counterNames = defaultCounterNames;
   20:              }
   21:              
   22:              CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
   23:              
   24:              //TODO: Need to remove this if HTTPS is enabled - this allows connecting though http, otherwise the connection will fail.
   25:              DeploymentDiagnosticManager.AllowInsecureRemoteConnections = true;
   26:              
   27:              //Get the diagnostis manager associated with this blob storage.
   28:              DeploymentDiagnosticManager deploymentDiagnosticsManager = new DeploymentDiagnosticManager(cloudStorageAccount, deploymentId);
   29:              
   30:              //Get the role instance diagnostics manager for all instance of the a role
   31:              var roles = deploymentDiagnosticsManager.GetRoleNames();
   32:              foreach (string item in roles)
   33:              {
   34:                  //Get the Role instance Diagnostics manager for each instance. and use it to enable data collection
   35:                  var roleInstanceManagers = deploymentDiagnosticsManager.GetRoleInstanceDiagnosticManagersForRole(item);
   36:                  RoleInstanceDiagnosticManager.AllowInsecureRemoteConnections = true;
   37:                  Console.Out.WriteLine("Getting Diagnostics Managers for Azure Role '{0}'", item);
   38:   
   39:                  //Set the new diagnostic monitor configuration for each instance of the role 
   40:                  foreach (var ridmN in roleInstanceManagers)
   41:                  {
   42:                      Console.Out.WriteLine("\tEnabling counters on instance {0} of role {1}", ridmN.RoleInstanceId, ridmN.RoleName);
   43:   
   44:                      EnableCounters(sampleRateInSeconds, transferPeriodInSeconds, counterNames, ridmN);
   45:                   }
   46:              }
   47:          }

  This is the code that enables the counters for each role instance:

    1:  private static void EnableCounters(int sampleRateInSeconds, int transferPeriodInSeconds, string[] counterNames, RoleInstanceDiagnosticManager ridmN)
    2:          {
    3:              DiagnosticMonitorConfiguration dmc = ridmN.GetCurrentConfiguration();// Depending on what you are trying to fo this can be DiagnosticMonitor.GetDefaultInitialConfiguration();
    4:                     
    5:              counterNames.ToList<String>().ForEach(counterName =>
    6:              {
    7:                  string counterNameTrimmed = counterName.Trim();
    8:                  if (!string.IsNullOrEmpty(counterNameTrimmed))
    9:                  {
   10:                      PerformanceCounterConfiguration pcc = new PerformanceCounterConfiguration();
   11:                      pcc.CounterSpecifier = counterNameTrimmed;
   12:                      pcc.SampleRate = TimeSpan.FromSeconds(sampleRateInSeconds);
   13:                      dmc.PerformanceCounters.DataSources.Add(pcc);
   14:                      Console.Out.WriteLine("\t\tCounter {0} Sample Rate {1} seconds", counterName, sampleRateInSeconds);
   15:                  }
   16:              });
   17:              
   18:              dmc.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromSeconds(transferPeriodInSeconds);
   19:              ridmN.SetCurrentConfiguration(dmc);
   20:          }