Remotely Change the Azure Diagnostic Monitor Configuration in Azure SDK 2.4 and earlier

Warning

This article only applies to diagnostics in the Azure SDK 2.4 and earlier.

After you have deployed a cloud service you can remotely change the configuration of the diagnostic monitor from code running in an application outside of Windows Azure using the DeploymentDiagnosticManager class. From this class, you can modify the diagnostic monitors for one or more role instances. You can also change the configuration of the diagnostic monitor for just one role instance using the RoleInstanceDiagnosticManager class.

Note

This topic applies to Azure SDK 2.4 and below.

Remotely change the diagnostic monitor configuration

The following example remotely updates the collection of two performance counters in a cloud service. Add a project reference to Microsoft.WindowsAzure.Diagnostics.dll, which is installed to the %Program Files%\Microsoft SDKs\Windows Azure.NET SDK\<version-num>\ref\ directory.

using Microsoft.WindowsAzure.Diagnostics; 
using Microsoft.WindowsAzure.Diagnostics.Management;

...

// Get the connection string. It's recommended that you store the connection string in your web.config or app.config file.
// Use the ConfigurationManager type to retrieve your storage connection string.  You can find the account name and key in
// the Windows Azure Management Portal (https://manage.windowsazure.com).
//string connectionString = "DefaultEndpointsProtocol=https;AccountName=<AccountName>;AccountKey=<AccountKey>";
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString;

// The deployment ID and role name for your application can be obtained from the Windows Azure Management Portal (https://manage.windowsazure.com).  
// See your application dashboard under Cloud Services.
string deploymentID = "28267ed8caf3424eab3f0b01acdd7281";
string roleName = "WebRole1";

// Get the DeploymentDiagnosticManager object for your deployment.
DeploymentDiagnosticManager diagManager = new DeploymentDiagnosticManager(connectionString, deploymentID);

// Get the RoleInstanceDiagnosticManager objects for each instance of your role.
IEnumerable<RoleInstanceDiagnosticManager> instanceManagers = diagManager.GetRoleInstanceDiagnosticManagersForRole(roleName);

// Update the sample rate for some performance counters.
PerformanceCounterConfiguration pccCPU = new PerformanceCounterConfiguration();
pccCPU.CounterSpecifier = @"\Processor(_Total)\% Processor Time";
pccCPU.SampleRate = TimeSpan.FromSeconds(20);

PerformanceCounterConfiguration pccMemory = new PerformanceCounterConfiguration();
pccMemory.CounterSpecifier = @"\Memory\Available Mbytes";
pccMemory.SampleRate = TimeSpan.FromSeconds(20);

// Iterate through the role instances and update the configuration.
foreach (var roleInstance in instanceManagers)
{
   DiagnosticMonitorConfiguration currentConfiguration = roleInstance.GetCurrentConfiguration();

   currentConfiguration.PerformanceCounters.DataSources.Add(pccCPU);
   currentConfiguration.PerformanceCounters.DataSources.Add(pccMemory);

   roleInstance.SetCurrentConfiguration(currentConfiguration);
}