使用 PowerShell 在 Azure 云服务中启用诊断Enable diagnostics in Azure Cloud Services using PowerShell

可以使用 Azure 诊断扩展从云服务收集应用程序日志、性能计数器等诊断数据。You can collect diagnostic data like application logs, performance counters etc. from a Cloud Service using the Azure Diagnostics extension. 本文介绍如何使用 PowerShell 为云服务启用 Azure 诊断扩展。This article describes how to enable the Azure Diagnostics extension for a Cloud Service using PowerShell. 有关本文所需的先决条件,请参阅如何安装和配置 Azure PowerShellSee How to install and configure Azure PowerShell for the prerequisites needed for this article.

在部署云服务过程中启用诊断扩展Enable diagnostics extension as part of deploying a Cloud Service

此方法适合持续集成类型的方案,在这些方案中,可以在部署云服务的过程中启用诊断扩展。This approach is applicable to continuous integration type of scenarios, where the diagnostics extension can be enabled as part of deploying the cloud service. 创建新的云服务部署时,可以通过向 New-AzureDeployment cmdlet 传入 ExtensionConfiguration 参数启用诊断扩展。When creating a new Cloud Service deployment you can enable the diagnostics extension by passing in the ExtensionConfiguration parameter to the New-AzureDeployment cmdlet. ExtensionConfiguration 参数取值为可使用 New-AzureServiceDiagnosticsExtensionConfig cmdlet 创建的诊断配置数组。The ExtensionConfiguration parameter takes an array of diagnostics configurations that can be created using the New-AzureServiceDiagnosticsExtensionConfig cmdlet.

以下示例演示如何为某个云服务(其中的 WebRole 和 WorkerRole 拥有不同的诊断配置)启用诊断。The following example shows how you can enable diagnostics for a cloud service with a WebRole and WorkerRole, each having a different diagnostics configuration.

$service_name = "MyService"
$service_package = "CloudService.cspkg"
$service_config = "ServiceConfiguration.Cloud.cscfg"
$webrole_diagconfigpath = "MyService.WebRole.PubConfig.xml" 
$workerrole_diagconfigpath = "MyService.WorkerRole.PubConfig.xml"

$webrole_diagconfig = New-AzureServiceDiagnosticsExtensionConfig -Role "WebRole" -DiagnosticsConfigurationPath $webrole_diagconfigpath
$workerrole_diagconfig = New-AzureServiceDiagnosticsExtensionConfig -Role "WorkerRole" -DiagnosticsConfigurationPath $workerrole_diagconfigpath

New-AzureDeployment -ServiceName $service_name -Slot Production -Package $service_package -Configuration $service_config -ExtensionConfiguration @($webrole_diagconfig,$workerrole_diagconfig) 

如果诊断配置文件使用某个存储帐户名称指定了 StorageAccount 元素,则 New-AzureServiceDiagnosticsExtensionConfig cmdlet 会自动使用该存储帐户。If the diagnostics configuration file specifies a StorageAccount element with a storage account name, then the New-AzureServiceDiagnosticsExtensionConfig cmdlet will automatically use that storage account. 要使其工作,存储帐户需要与要部署的云服务位于同一订阅中。For this to work, the storage account needs to be in the same subscription as the Cloud Service being deployed.

从 Azure SDK 2.6 开始,MSBuild 发布目标输出生成的扩展配置文件将基于服务配置文件 (.cscfg) 中指定的诊断配置字符串包括存储帐户名称。From Azure SDK 2.6 onward the extension configuration files generated by the MSBuild publish target output will include the storage account name based on the diagnostics configuration string specified in the service configuration file (.cscfg). 以下脚本演示了在部署云服务时如何分析发布目标输出中的扩展配置文件并为每个角色配置诊断扩展。The script below shows you how to parse the Extension configuration files from the publish target output and configure diagnostics extension for each role when deploying the cloud service.

$service_name = "MyService"
$service_package = "C:\build\output\CloudService.cspkg"
$service_config = "C:\build\output\ServiceConfiguration.Cloud.cscfg"

#Find the Extensions path based on service configuration file
$extensionsSearchPath = Join-Path -Path (Split-Path -Parent $service_config) -ChildPath "Extensions"

$diagnosticsExtensions = Get-ChildItem -Path $extensionsSearchPath -Filter "PaaSDiagnostics.*.PubConfig.xml"
$diagnosticsConfigurations = @()
foreach ($extPath in $diagnosticsExtensions)
{
#Find the RoleName based on file naming convention PaaSDiagnostics.<RoleName>.PubConfig.xml
$roleName = ""
$roles = $extPath -split ".",0,"simplematch"
if ($roles -is [system.array] -and $roles.Length -gt 1)
    {
    $roleName = $roles[1] 
    $x = 2
    while ($x -le $roles.Length)
        {
           if ($roles[$x] -ne "PubConfig")
            {
                $roleName = $roleName + "." + $roles[$x]
            }
            else
            {
                break
            }
            $x++
        }
    $fullExtPath = Join-Path -path $extensionsSearchPath -ChildPath $extPath
    $diagnosticsconfig = New-AzureServiceDiagnosticsExtensionConfig -Role $roleName -DiagnosticsConfigurationPath $fullExtPath
    $diagnosticsConfigurations += $diagnosticsconfig
    }
}
New-AzureDeployment -ServiceName $service_name -Slot Production -Package $service_package -Configuration $service_config -ExtensionConfiguration $diagnosticsConfigurations

Visual Studio Online 使用类似的方法通过诊断扩展自动部署云服务。Visual Studio Online uses a similar approach for automated deployments of Cloud Services with the diagnostics extension. 有关完整示例,请参阅 Publish-AzureCloudDeployment.ps1See Publish-AzureCloudDeployment.ps1 for a complete example.

如果在诊断配置中未指定 StorageAccount ,则需要将 StorageAccountName 参数传递给 cmdlet。If no StorageAccount was specified in the diagnostics configuration, then you need to pass in the StorageAccountName parameter to the cmdlet. 如果指定了 StorageAccountName 参数,cmdlet 始终使用该参数中指定的存储帐户,而不使用诊断配置文件中指定的存储帐户。If the StorageAccountName parameter is specified, then the cmdlet will always use the storage account that is specified in the parameter and not the one that is specified in the diagnostics configuration file.

如果诊断存储帐户与云服务属于不同的订阅,则需要将 StorageAccountName 和 StorageAccountKey 参数显式传递给 cmdlet。If the diagnostics storage account is in a different subscription from the Cloud Service, then you need to explicitly pass in the StorageAccountName and StorageAccountKey parameters to the cmdlet. 当诊断存储帐户在同一订阅中时,不需要 StorageAccountKey 参数,因为 cmdlet 可以在启用诊断扩展时自动查询和设置密钥值。The StorageAccountKey parameter is not needed when the diagnostics storage account is in the same subscription, as the cmdlet can automatically query and set the key value when enabling the diagnostics extension. 但是,如果诊断存储帐户在不同的订阅中,则 cmdlet 可能无法自动获取密钥,需要通过 StorageAccountKey 参数显式指定该密钥。However, if the diagnostics storage account is in a different subscription, then the cmdlet might not be able to get the key automatically and you need to explicitly specify the key through the StorageAccountKey parameter.

$webrole_diagconfig = New-AzureServiceDiagnosticsExtensionConfig -Role "WebRole" -DiagnosticsConfigurationPath $webrole_diagconfigpath -StorageAccountName $diagnosticsstorage_name -StorageAccountKey $diagnosticsstorage_key
$workerrole_diagconfig = New-AzureServiceDiagnosticsExtensionConfig -Role "WorkerRole" -DiagnosticsConfigurationPath $workerrole_diagconfigpath -StorageAccountName $diagnosticsstorage_name -StorageAccountKey $diagnosticsstorage_key

在现有的云服务上启用诊断扩展Enable diagnostics extension on an existing Cloud Service

可以使用 Set-AzureServiceDiagnosticsExtension cmdlet 在已运行的云服务上启用或更新诊断配置。You can use the Set-AzureServiceDiagnosticsExtension cmdlet to enable or update diagnostics configuration on a Cloud Service that is already running.

警告

为现有角色启用诊断时,将禁用部署包时已设置的所有扩展。When you enable diagnostics for an existing role, any extensions that you have already set are disabled when the package is deployed. 其中包括:These include:

  • Microsoft Monitoring Agent 诊断Microsoft Monitoring Agent Diagnostics
  • Microsoft Azure 安全监视Microsoft Azure Security Monitoring
  • Microsoft AntimalwareMicrosoft Antimalware
  • Microsoft Monitoring AgentMicrosoft Monitoring Agent
  • Microsoft 服务探查器代理Microsoft Service Profiler Agent
  • Microsoft Azure 域扩展Windows Azure Domain Extension
  • Microsoft Azure 诊断扩展Windows Azure Diagnostics Extension
  • Microsoft Azure 远程桌面扩展Windows Azure Remote Desktop Extension
  • Microsoft Azure 日志收集器Windows Azure Log Collector

部署更新的角色后,可以通过 Azure 门户或 PowerShell 重置扩展。You can reset your extensions via the Azure portal or PowerShell after you deploy the updated role.

$service_name = "MyService"
$webrole_diagconfigpath = "MyService.WebRole.PubConfig.xml" 
$workerrole_diagconfigpath = "MyService.WorkerRole.PubConfig.xml"

$webrole_diagconfig = New-AzureServiceDiagnosticsExtensionConfig -Role "WebRole" -DiagnosticsConfigurationPath $webrole_diagconfigpath
$workerrole_diagconfig = New-AzureServiceDiagnosticsExtensionConfig -Role "WorkerRole" -DiagnosticsConfigurationPath $workerrole_diagconfigpath

Set-AzureServiceDiagnosticsExtension -DiagnosticsConfiguration @($webrole_diagconfig,$workerrole_diagconfig) -ServiceName $service_name 

获取当前诊断扩展配置Get current diagnostics extension configuration

使用 Get AzureServiceDiagnosticsExtension cmdlet 可以获取云服务的当前诊断配置。Use the Get-AzureServiceDiagnosticsExtension cmdlet to get the current diagnostics configuration for a cloud service.

Get-AzureServiceDiagnosticsExtension -ServiceName "MyService"

删除诊断扩展Remove diagnostics extension

若要在云服务上关闭诊断,可以使用 Remove-AzureServiceDiagnosticsExtension cmdlet。To turn off diagnostics on a cloud service you can use the Remove-AzureServiceDiagnosticsExtension cmdlet.

Remove-AzureServiceDiagnosticsExtension -ServiceName "MyService"

如果使用不带 Role 参数的 Set-AzureServiceDiagnosticsExtension 或 New-AzureServiceDiagnosticsExtensionConfig 启用了诊断扩展,则可以使用不带 Role 参数的 Remove-AzureServiceDiagnosticsExtension 删除该扩展 。If you enabled the diagnostics extension using either Set-AzureServiceDiagnosticsExtension or the New-AzureServiceDiagnosticsExtensionConfig without the Role parameter then you can remove the extension using Remove-AzureServiceDiagnosticsExtension without the Role parameter. 如果启用扩展时使用了 Role 参数,则删除扩展时也必须使用该参数。If the Role parameter was used when enabling the extension then it must also be used when removing the extension.

若要从单个角色中删除诊断扩展,请使用以下命令:To remove the diagnostics extension from each individual role:

Remove-AzureServiceDiagnosticsExtension -ServiceName "MyService" -Role "WebRole"

后续步骤Next Steps