使用 PowerShell 为 Azure 云服务设置 Application InsightsUsing PowerShell to set up Application Insights for Azure Cloud Services

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

可以将 Azure 配置为向 Azure Application Insights 发送 Azure 诊断Azure can be configured to send Azure Diagnostics to Azure Application Insights. 该诊断与 Azure Cloud Service 和 Azure VM 有关。The diagnostics relate to Azure Cloud Services and Azure VMs. 它们是对使用 Application Insights SDK 从应用内发送的遥测的补充。They complement the telemetry that you send from within the app using the Application Insights SDK. 作为在 Azure 中自动处理新建资源过程的一部分,可以使用 PowerShell 配置诊断。As part of automating the process of creating new resources in Azure, you can configure diagnostics using PowerShell.

Azure 模板Azure template

如果 Web 应用在 Azure 中,并且使用 Azure 资源管理器模板创建资源,可以通过将以下内容添加到资源节点来配置 Application Insights:If the web app is in Azure and you create your resources using an Azure Resource Manager template, you can configure Application Insights by adding this to the resources node:

{
  resources: [
    /* Create Application Insights resource */
    {
      "apiVersion": "2015-05-01",
      "type": "microsoft.insights/components",
      "name": "nameOfAIAppResource",
      "location": "chinaeast2",
      "kind": "web",
      "properties": { "ApplicationId": "nameOfAIAppResource" },
      "dependsOn": [
        "[concat('Microsoft.Web/sites/', myWebAppName)]"
      ]
    }
  ]
}
  • nameOfAIAppResource - Application Insights 资源的名称nameOfAIAppResource - a name for the Application Insights resource
  • myWebAppName - Web 应用的 IDmyWebAppName - the ID of the web app

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

New-AzureDeployment cmdlet 具有参数 ExtensionConfiguration,它会接收一组诊断配置。The New-AzureDeployment cmdlet has a parameter ExtensionConfiguration, which takes an array of diagnostics configurations. 可使用 New-AzureServiceDiagnosticsExtensionConfig cmdlet 创建这些诊断配置。These can be created using the New-AzureServiceDiagnosticsExtensionConfig cmdlet. 例如:For example:

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

$primary_storagekey = (Get-AzStorageKey `
  -StorageAccountName "$diagnostics_storagename").Primary
$storage_context = New-AzStorageContext `
  -StorageAccountName $diagnostics_storagename `
  -StorageAccountKey $primary_storagekey

$webrole_diagconfig = `
  New-AzureServiceDiagnosticsExtensionConfig `
    -Role "WebRole" -Storage_context $storageContext `
    -DiagnosticsConfigurationPath $webrole_diagconfigpath
$workerrole_diagconfig = `
  New-AzureServiceDiagnosticsExtensionConfig `
    -Role "WorkerRole" `
    -StorageContext $storage_context `
    -DiagnosticsConfigurationPath $workerrole_diagconfigpath

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

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

在现有服务上,使用 Set-AzureServiceDiagnosticsExtensionOn an existing service, use Set-AzureServiceDiagnosticsExtension.

$service_name = "MyService"
$diagnostics_storagename = "myservicediagnostics"
$webrole_diagconfigpath = "MyService.WebRole.PubConfig.xml" 
$workerrole_diagconfigpath = "MyService.WorkerRole.PubConfig.xml"
$primary_storagekey = (Get-AzStorageKey `
  -StorageAccountName "$diagnostics_storagename").Primary
$storage_context = New-AzStorageContext `
  -StorageAccountName $diagnostics_storagename `
  -StorageAccountKey $primary_storagekey

Set-AzureServiceDiagnosticsExtension `
  -StorageContext $storage_context `
  -DiagnosticsConfigurationPath $webrole_diagconfigpath `
  -ServiceName $service_name `
  -Slot Production `
  -Role "WebRole" 
Set-AzureServiceDiagnosticsExtension `
  -StorageContext $storage_context `
  -DiagnosticsConfigurationPath $workerrole_diagconfigpath `
  -ServiceName $service_name `
  -Slot Production `
  -Role "WorkerRole"

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

Get-AzureServiceDiagnosticsExtension -ServiceName "MyService"

删除诊断扩展Remove diagnostics extension

Remove-AzureServiceDiagnosticsExtension -ServiceName "MyService"

如果已使用不带 Role 参数的 Set-AzureServiceDiagnosticsExtensionNew-AzureServiceDiagnosticsExtensionConfig 启用诊断扩展,则可以使用不带 Role 参数的 Remove-AzureServiceDiagnosticsExtension 删除该扩展。If you enabled the diagnostics extension using either Set-AzureServiceDiagnosticsExtension or 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"

另请参阅See also