在 Azure 虚拟机和 Azure 虚拟机规模集上部署 Azure Monitor Application Insights 代理Deploy the Azure Monitor Application Insights Agent on Azure virtual machines and Azure virtual machine scale sets

Azure 虚拟机上运行的基于 .NET 的 Web 应用程序上启用监视。Enabling monitoring on your .NET based web applications running on Azure virtual machines. 获取使用 Application Insights 的所有权益,不需修改代码。Get all the benefits of using Application Insights without modifying your code.

本文逐步讲解如何通过 Application Insights 代理启用 Application Insights 监视,并提供有关如何自动完成大规模部署过程的初步指导。This article walks you through enabling Application Insights monitoring using the Application Insights Agent and provides preliminary guidance for automating the process for large-scale deployments.

重要

用于 .NET 的 Azure Application Insights 代理目前为公共预览版。Azure Application Insights Agent for .NET is currently in public preview. 此预览版在提供时没有附带服务级别协议,我们不建议将其用于生产工作负荷。This preview version is provided without a service-level agreement, and we don't recommend it for production workloads. 有些功能可能不受支持,有些功能可能受到限制。Some features might not be supported, and some might have constrained capabilities.

启用 Application InsightsEnable Application Insights

可通过两种方法为 Azure 虚拟机和 Azure 虚拟机规模集托管的应用程序启用应用程序监视:There are two ways to enable application monitoring for Azure virtual machines and Azure virtual machine scale sets hosted applications:

  • 通过 Application Insights 代理执行的“无代码” 方法Codeless via Application Insights Agent

    • 这是启用监视的最简单方法,无需完成任何高级配置。This method is the easiest to enable, and no advanced configuration is required. 这种监视通常称为“运行时”监视。It is often referred to as "runtime" monitoring.

    • 对于 Azure 虚拟机和 Azure 虚拟机规模集,建议至少启用此级别的监视。For Azure virtual machines and Azure virtual machine scale sets we recommend at a minimum enabling this level of monitoring. 然后,可以根据具体情况评估是否需要手动检测。After that, based on your specific scenario, you can evaluate whether manual instrumentation is needed.

    • Application Insights 代理自动收集与 .NET SDK 相同的、现成的依赖项信号。The Application Insights Agent auto-collects the same dependency signals out-of-the-box as the .NET SDK. 若要了解详细信息,请参阅依赖项自动收集See Dependency auto-collection to learn more.

      备注

      目前仅支持 .Net IIS 托管的应用程序。Currently only .Net IIS-hosted applications are supported. 请使用 SDK 检测托管在 Azure 虚拟机和虚拟机规模集上的 ASP.NET Core、Java 和 Node.js 应用程序。Use an SDK to instrument ASP.NET Core, Java, and Node.js applications hosted on an Azure virtual machines and virtual machine scale sets.

  • 通过 SDK 执行的“基于代码”方法Code-based via SDK

备注

如果同时检测到了基于代理的监视和基于手动 SDK 的检测,则只会遵循手动检测设置,If both agent based monitoring and manual SDK based instrumentation is detected only the manual instrumentation settings will be honored. 这是为了防止发送重复数据。This is to prevent duplicate data from being sent. 有关详细信息,请查看下面的故障排除部分To learn more about this check out the troubleshooting section below.

使用 PowerShell 在 Azure 虚拟机上管理用于 .NET 应用程序的 Application Insights 代理Manage Application Insights Agent for .NET applications on Azure virtual machines using PowerShell

备注

在安装 Application Insights 代理之前,需要一个连接字符串。Before installing the Application Insights Agent, you'll need a connection string. 创建新的 Application Insights 资源,或从现有 Application Insights 资源复制连接字符串。Create a new Application Insights Resource or copy the connection string from an existing application insights resource.

备注

不熟悉 PowerShell?New to powershell? 请查看入门指南Check out the Get Started Guide.

安装或更新用作 Azure 虚拟机扩展的 Application Insights 代理Install or update the Application Insights Agent as an extension for Azure virtual machines

$publicCfgJsonString = '
{
  "redfieldConfiguration": {
    "instrumentationKeyMap": {
      "filters": [
        {
          "appFilter": ".*",
          "machineFilter": ".*",
          "virtualPathFilter": ".*",
          "instrumentationSettings" : {
            "connectionString": "InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
          }
        }
      ]
    }
  }
}
';
$privateCfgJsonString = '{}';

Set-AzVMExtension -ResourceGroupName "<myVmResourceGroup>" -VMName "<myVmName>" -Location "<myVmLocation>" -Name "ApplicationMonitoring" -Publisher "Microsoft.Azure.Diagnostics" -Type "ApplicationMonitoringWindows" -Version "2.8" -SettingString $publicCfgJsonString -ProtectedSettingString $privateCfgJsonString

备注

可以使用 PowerShell 循环跨多个虚拟机大规模安装或更新用作扩展的 Application Insights 代理。You may install or update the Application Insights Agent as an extension across multiple Virtual Machines at-scale using a Powershell loop.

从 Azure 虚拟机卸载 Application Insights 代理扩展Uninstall Application Insights Agent extension from Azure virtual machine

Remove-AzVMExtension -ResourceGroupName "<myVmResourceGroup>" -VMName "<myVmName>" -Name "ApplicationMonitoring"

查询 Azure 虚拟机的 Application Insights 代理扩展状态Query Application Insights Agent extension status for Azure virtual machine

Get-AzVMExtension -ResourceGroupName "<myVmResourceGroup>" -VMName "<myVmName>" -Name ApplicationMonitoring -Status

获取 Azure 虚拟机的已安装扩展的列表Get list of installed extensions for Azure virtual machine

Get-AzResource -ResourceId "/subscriptions/<mySubscriptionId>/resourceGroups/<myVmResourceGroup>/providers/Microsoft.Compute/virtualMachines/<myVmName>/extensions"

# Name              : ApplicationMonitoring
# ResourceGroupName : <myVmResourceGroup>
# ResourceType      : Microsoft.Compute/virtualMachines/extensions
# Location          : China East
# ResourceId        : /subscriptions/<mySubscriptionId>/resourceGroups/<myVmResourceGroup>/providers/Microsoft.Compute/virtualMachines/<myVmName>/extensions/ApplicationMonitoring

备注

验证安装的方法是:单击与你在部署 Application Insights 代理扩展时使用的检测密钥相关联的 Application Insights 资源中的“实时指标流”。Verify installation by clicking on Live Metrics Stream within the Application Insights Resource associated with the instrumentation key you used to deploy the Application Insights Agent Extension. 如果从多个虚拟机发送数据,请在“服务器名称”下选择目标 Azure 虚拟机。If you are sending data from multiple Virtual Machines, select the target Azure virtual machines under Server Name. 可能需要等待长达一分钟的时间数据才会开始流动。It may take up to a minute for data to begin flowing.

使用 PowerShell 在 Azure 虚拟机规模集上管理用于 .NET 应用程序的 Application Insights 代理Manage Application Insights Agent for .NET applications on Azure virtual machine scale sets using powershell

安装或更新用作 Azure 虚拟机规模集扩展的 Application Insights 代理Install or update the Application Insights Agent as an extension for Azure virtual machine scale set

$publicCfgHashtable =
@{
  "redfieldConfiguration"= @{
    "instrumentationKeyMap"= @{
      "filters"= @(
        @{
          "appFilter"= ".*";
          "machineFilter"= ".*";
          "virtualPathFilter": ".*",
          "instrumentationSettings" : {
            "connectionString": "InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # Application Insights connection string, create new Application Insights resource if you don't have one. https://ms.portal.azure.com/#blade/HubsExtension/BrowseResourceBlade/resourceType/microsoft.insights%2Fcomponents
          }
        }
      )
    }
  }
};
$privateCfgHashtable = @{};

$vmss = Get-AzVmss -ResourceGroupName "<myResourceGroup>" -VMScaleSetName "<myVmssName>"

Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "ApplicationMonitoring" -Publisher "Microsoft.Azure.Diagnostics" -Type "ApplicationMonitoringWindows" -TypeHandlerVersion "2.8" -Setting $publicCfgHashtable -ProtectedSetting $privateCfgHashtable

Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -Name $vmss.Name -VirtualMachineScaleSet $vmss

# Note: depending on your update policy, you might need to run Update-AzVmssInstance for each instance

从 Azure 虚拟机规模集卸载应用程序监视扩展Uninstall application monitoring extension from Azure virtual machine scale sets

$vmss = Get-AzVmss -ResourceGroupName "<myResourceGroup>" -VMScaleSetName "<myVmssName>"

Remove-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "ApplicationMonitoring"

Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -Name $vmss.Name -VirtualMachineScaleSet $vmss

# Note: depending on your update policy, you might need to run Update-AzVmssInstance for each instance

查询 Azure 虚拟机规模集的应用程序监视扩展状态Query application monitoring extension status for Azure virtual machine scale set

# Not supported by extensions framework

获取 Azure 虚拟机规模集的已安装扩展的列表Get list of installed extensions for Azure virtual machine scale set

Get-AzResource -ResourceId /subscriptions/<mySubscriptionId>/resourceGroups/<myResourceGroup>/providers/Microsoft.Compute/virtualMachineScaleSets/<myVmssName>/extensions

# Name              : ApplicationMonitoringWindows
# ResourceGroupName : <myResourceGroup>
# ResourceType      : Microsoft.Compute/virtualMachineScaleSets/extensions
# Location          :
# ResourceId        : /subscriptions/<mySubscriptionId>/resourceGroups/<myResourceGroup>/providers/Microsoft.Compute/virtualMachineScaleSets/<myVmssName>/extensions/ApplicationMonitoringWindows

故障排除Troubleshooting

请查找在 Azure 虚拟机和虚拟机规模集上运行的 .NET 应用程序的 Application Insights 监视代理扩展的故障排除提示。Find troubleshooting tips for Application Insights Monitoring Agent Extension for .NET applications running on Azure virtual machines and virtual machine scale sets.

备注

仅支持通过基于 SDK 的手动检测在 Azure 虚拟机和 Azure 虚拟机规模集中使用 .NET Core、Java 和 Node.js 应用程序,因此,以下步骤不适用于这些方案。.NET Core, Java, and Node.js applications are only supported on Azure virtual machines and Azure virtual machine scale sets via manual SDK based instrumentation and therefore the steps below do not apply to these scenarios.

扩展执行输出将记录到在以下目录中发现的文件:Extension execution output is logged to files found in the following directories:

C:\WindowsAzure\Logs\Plugins\Microsoft.Azure.Diagnostics.ApplicationMonitoringWindows\<version>\

后续步骤Next steps