從遠端變更診斷監視器組態

更新日期: 2015年11月

當您部署雲端服務之後,您也可以在遠端使用 DeploymentDiagnosticManager 類別從 Windows Azure 外面的應用程式中執行的程式碼來變更診斷監視器的組態。您可以從這個類別修改一個或多個角色執行個體的診斷監視器。您也可以使用 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);
}

顯示: