导出 (0) 打印
全部展开

Azure 诊断 (SDK 2.0) 中的重大更改

更新时间: 2014年12月

Microsoft Azure SDK for.NET 2.0 版本中的 Microsoft Azure Diagnostics 库 (Microsoft.WindowsAzure.Diagnostics.dll) 与版本 1.8 库相比有了重大变化。更新了 Microsoft Azure 诊断客户端库,使其不再依赖存储客户端库版本 1.7,这可能需要在你的代码中进行相应的更改。此项更改使你的应用程序更容易使用更新版本的存储客户端库。本主题提供了有关 1.8 版和 2.0 版之间已发生的更改的信息,以及用于帮助你将现有应用程序迁移到 2.0 版的迁移提示。

已从 Microsoft.WindowsAzure.DiagnosticsMicrosoft.WindowsAzure.Diagnostics.Management API 中删除了对存储客户端库的依赖性。有多种方法接受使用 CloudStorageAccount 对象作为输入参数,以指定与应用程序关联的 Microsoft Azure 存储帐户(用于存储应用程序诊断配置的存储帐户)。取代这些方法的新方法现在接受使用连接字符串而不是 CloudStorageAccount 对象来指定存储帐户。

下表显示了 Microsoft.WindowsAzure.Diagnostics.Management 命名空间中已过时方法和新方法之间的映射:

 

版本 1.8(已过时) 版本 2.0(新)

CloudAccountDiagnosticMonitorExtensions.CreateDeploymentDiagnosticManager(this CloudStorageAccount storageAccount,string deploymentId)

CreateDeploymentDiagnosticManager

CloudAccountDiagnosticMonitorExtensions.CreateRoleInstanceDiagnosticManager(this CloudStorageAccount storageAccount, string deploymentId, string roleName, string roleInstanceId)

CreateRoleInstanceDiagnosticManager

OnDemandTransferInfo.FromQueueMessage(CloudQueueMessage queueMessage)

FromQueueMessage

DeploymentDiagnosticManager.DeploymentDiagnosticManager(CloudStorageAccount storageAccount, string deploymentId)

DeploymentDiagnosticManager

RoleInstanceDiagnosticManager(CloudStorageAccount storageAccount, string deploymentId, string roleName, string roleInstanceId)

RoleInstanceDiagnosticManager

下表显示了 Microsoft.WindowsAzure.Diagnostics 命名空间中已过时方法和新方法之间的映射:

Microsoft.WindowsAzure.Diagnostics

版本 1.8(已过时) 版本 2.0(新)

DiagnosticMonitor.Start(CloudStorageAccount storageAccount, DiagnosticMonitorConfiguration initialConfiguration)

StartWithConnectionString

DiagnosticMonitor.UpdateStorageAccount(CloudStorageAccount storageAccount)

UpdateStorageAccount

以下示例将使用 DeploymentDiagnosticManager 对象远程更改特定角色中所有实例的诊断配置。启用了两个性能计数器用于保存集合和配置更改。此示例使用 Microsoft.WindowsAzure.Diagnostics.dll 程序集版本 1.8,DeploymentDiagnosticManager 构造函数接受 CloudStorageAccount 对象作为输入参数。CloudStorageAccount 对象用于指定存储应用程序诊断配置的存储帐户。

// 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 = "2f10410d4a194b0fa8863365674828ba";
string roleName = "WebRole1";

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

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

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

// Add a performance counter for processor time.
PerformanceCounterConfiguration pccCPU = new PerformanceCounterConfiguration();
pccCPU.CounterSpecifier = @"\Processor(_Total)\% Processor Time";
pccCPU.SampleRate = perfSampleRate;

// Add a performance counter for available memory.
PerformanceCounterConfiguration pccMemory = new PerformanceCounterConfiguration();
pccMemory.CounterSpecifier = @"\Memory\Available Mbytes";
pccMemory.SampleRate = perfSampleRate;

// 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);

   //Update the configuration
   roleInstance.SetCurrentConfiguration(currentConfiguration);
}

下面显示了重新编写的示例,现在它使用 Microsoft.WindowsAzure.Diagnostics.dll 程序集版本 2.0。DeploymentDiagnosticManager 构造函数现在接受使用连接字符串而不是 CloudStorageAccount 对象来指定存储帐户。

// 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 = "e2ab8b6667644666ba627bdf6f5e4daa";
string roleName = "WebRole1";

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

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

// Use 45 seconds for the performance counter sample rate.
TimeSpan perfSampleRate = TimeSpan.FromSeconds(45.0);

// Add a performance counter for processor time.
PerformanceCounterConfiguration pccCPU = new PerformanceCounterConfiguration();
pccCPU.CounterSpecifier = @"\Processor(_Total)\% Processor Time";
pccCPU.SampleRate = perfSampleRate;

// Add a performance counter for available memory.
PerformanceCounterConfiguration pccMemory = new PerformanceCounterConfiguration();
pccMemory.CounterSpecifier = @"\Memory\Available Mbytes";
pccMemory.SampleRate = perfSampleRate;

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

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

   roleInstance.SetCurrentConfiguration(currentConfiguration);
}

显示:
© 2015 Microsoft