您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

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

可以将 Microsoft Azure 配置为向 Azure Application Insights发送 Azure 诊断Microsoft 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": "centralus",
      "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