远程更改诊断监视器配置

更新时间: 2015年11月

在部署云服务后,你可以通过在 Windows Azure 外部的应用程序中运行的代码使用 DeploymentDiagnosticManager 类远程更改诊断监视器配置。从该类中,你可以修改一个或多个角色实例的诊断监视器。也可以使用 RoleInstanceDiagnosticManager 类仅更改一个角色实例的诊断监视器配置。

note备注
此主题适用于 Azure SDK 2.4 和更低版本。

下面的示例远程更新云服务中的两个性能计数器的集合。添加对 Microsoft.WindowsAzure.Diagnostics.dll(安装到 %Program Files%\Microsoft SDKs\Windows Azure.NET SDK\<版本号>\ref\ 目录中)的项目引用。

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 (http://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 (http://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);
}

显示: