共用方式為


使用 PowerShell 在 Azure 雲端服務中啟用診斷

重要

針對新客戶目前已取代 Azure 雲端服務 (傳統),而針對所有客戶,該服務將從 2024 年 8 月 31 日起完全淘汰。 新部署應該使用 Azure Resource Manager 型的新部署模型 Azure 雲端服務 (延伸支援)

您可以使用 Azure 診斷延伸模組,從雲端服務收集診斷資料 (例如應用程式記錄、效能計數器等)。 本文描述如何使用 PowerShell 啟用雲端服務的 Azure 診斷延伸模組。 如需這篇文章所需要的必要條件,請參閱 如何安裝和設定 Azure PowerShell

啟用診斷延伸模組做為部署雲端服務的一部分

此方法適用於可以啟用診斷延伸模組做為雲端服務佈署一部分的連續整合類型案例。 當您建立新的雲端服務部署時,可以啟用診斷擴充功能,方法是將 ExtensionConfiguration 參數傳入 New-AzureDeployment Cmdlet。 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 一定會使用在此參數中指定的儲存體帳戶,而非在診斷組態檔中指定的儲存體帳戶。

如果診斷儲存體帳戶和雲端服務分別屬於不同的訂用帳戶,您必須明確地將 StorageAccountNameStorageAccountKey 參數傳入 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 Monitoring Agent 診斷
  • Microsoft Azure 安全性監控
  • Microsoft Antimalware \(部分機器翻譯\)
  • Microsoft Monitoring Agent
  • Microsoft 服務分析工具代理程式
  • Windows Azure 網域擴充功能
  • Windows Azure 診斷擴充功能
  • Windows Azure 遠端桌面擴充功能
  • Windows 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"

後續步驟