使用 PowerShell 在 Azure 云服务(经典)中启用诊断

重要

新客户的云服务(经典版)现已弃用,并将于 2024 年 8 月 31 日对所有客户停用。 新部署应使用基于 Azure 资源管理器的新型部署模型 Azure 云服务(外延支持)

可以使用 Azure 诊断扩展从云服务收集应用程序日志、性能计数器等诊断数据。 本文介绍如何使用 PowerShell 为云服务启用 Azure 诊断扩展。 有关本文所需的先决条件,请参阅如何安装和配置 Azure PowerShell

在部署云服务过程中启用诊断扩展

此方法适合持续集成类型的方案,在这些方案中可以在部署云服务的过程中启用诊断扩展。 创建新的云服务部署时,可以通过向 New-AzureDeployment cmdlet 传入 ExtensionConfiguration 参数启用诊断扩展。 ExtensionConfiguration 参数取值为可通过 New-AzureServiceDiagnosticsExtensionConfig cmdlet 创建的诊断配置数组。

以下示例演示如何为某个云服务(其中的 WebRole 和 WorkerRole 拥有不同的诊断配置)启用诊断。

$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 会自动使用该存储帐户。 为此,存储帐户需要属于部署云服务的同一订阅。

从 Azure SDK 2.6 开始,MSBuild 发布目标输出生成的扩展配置文件将基于服务配置文件 (.cscfg) 中指定的诊断配置字符串包括存储帐户名称。 以下脚本演示了在部署云服务时如何分析发布目标输出中的扩展配置文件并为每个角色配置诊断扩展。

$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 使用类似的方法通过诊断扩展自动部署云服务。 有关完整示例,请参阅 Publish-AzureCloudDeployment.ps1

如果未在诊断配置中指定 StorageAccount,则需要将 StorageAccountName 参数传递给 cmdlet。 如果指定了 StorageAccountName 参数,cmdlet 始终使用该参数中指定的存储帐户,而不使用诊断配置文件中指定的存储帐户。

如果诊断存储帐户与云服务属于不同的订阅,则必须将 StorageAccountName 和 StorageAccountKey 参数显式传递给 cmdlet。 当诊断存储帐户属于同一订阅时,不需要 StorageAccountKey 参数,因为在启用诊断扩展的情况下,cmdlet 可以自动查询和设置密钥值。 但是,如果诊断存储帐户属于不同的订阅,则 cmdlet 可能无法自动获取密钥,必须通过 StorageAccountKey 参数显式指定该密钥。

$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

在现有的云服务上启用诊断扩展

可以使用 Set-AzureServiceDiagnosticsExtension cmdlet 在已运行的云服务上启用或更新诊断配置。

警告

为现有角色启用诊断时,将禁用部署包时已设置的所有扩展。 其中包括:

  • Microsoft 监视代理诊断
  • Azure 安全监视
  • Microsoft 反恶意软件
  • Microsoft Monitoring Agent
  • Microsoft 服务探查器代理
  • Azure 域扩展
  • Azure 诊断扩展
  • Azure 远程桌面扩展
  • Azure 日志收集器

部署更新的角色后,可以通过 Azure 门户或 PowerShell 重置扩展。

$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-AzureServiceDiagnosticsExtension cmdlet 可以获取云服务的当前诊断配置。

Get-AzureServiceDiagnosticsExtension -ServiceName "MyService"

删除诊断扩展

若要在云服务上关闭诊断,可以使用 Remove-AzureServiceDiagnosticsExtension cmdlet。

Remove-AzureServiceDiagnosticsExtension -ServiceName "MyService"

如果使用不带 Role 参数的 Set-AzureServiceDiagnosticsExtension 或 New-AzureServiceDiagnosticsExtensionConfig 启用了诊断扩展,则可以使用不带 Role 参数的 Remove-AzureServiceDiagnosticsExtension 删除该扩展 。 如果启用扩展时使用了 Role 参数,则删除扩展时也必须使用该参数。

若要从单个角色中删除诊断扩展,请使用以下命令:

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

后续步骤