你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

适用于 Azure VM 和虚拟机规模集的 Application Insights

对于在 Azure 虚拟机Azure 虚拟机规模集上运行的 ASP.NET 和 ASP.NET Core IIS 托管应用程序,现在为其启用监视比以往更容易。 获取使用 Application Insights 的所有权益,不需修改代码。

本文将逐步讲解如何使用 Application Insights 代理启用 Application Insights 监视。 它还为大规模部署过程的自动化提供了初步指导。

启用 Application Insights

可以轻松启用自动检测。 不需要高级配置。

有关支持的自动检测方案的完整列表,请参阅支持的环境、语言和资源提供程序

注意

自动检测功能适用于 ASP.NET、ASP.NET Core IIS 托管的应用程序和 Java。 使用 SDK 来检测托管在 Azure 虚拟机和虚拟机规模集上的 Node.js 和 Python 应用程序。

Application Insights 代理自动收集与 SDK 相同的、现成的依赖项信号。 若要了解详细信息,请参阅依赖项自动收集

在安装 Application Insights 代理扩展之前,需要一个连接字符串。 创建新的 Application Insights 资源,或从现有 Application Insights 资源复制连接字符串。

为虚拟机启用监视

可使用 Azure 门户或 PowerShell 来启用 VM 监视。

Azure 门户

  1. 在 Azure 门户中,转到你的 Application Insights 资源。 将你的连接字符串复制到剪贴板。

    显示连接字符串的屏幕截图。

  2. 转到你的虚拟机。 在左侧菜单的“设置”部分下,选择“扩展 + 应用程序”>“添加”。

    显示“扩展 + 应用程序”窗格的屏幕截图,其中包含“添加”按钮。

  3. 选择“Application Insights 代理”>“下一步”。

    显示“安装扩展”窗格的屏幕截图,其中包含“下一步”按钮。

  4. 粘贴你在步骤 1 中复制的连接字符串,然后选择“查看 + 创建”。

    显示“创建”选项卡的屏幕截图,其中包含“查看 + 创建”按钮。

PowerShell

注意

你是 PowerShell 新手吗? 请查看入门指南

安装或更新用作 Azure 虚拟机扩展的 Application Insights 代理:

# define variables to match your environment before running
$ResourceGroup = "<myVmResourceGroup>"
$VMName = "<myVmName>"
$Location = "<myVmLocation>"
$ConnectionString = "<myAppInsightsResourceConnectionString>"

$publicCfgJsonString = @"
{
    "redfieldConfiguration": {
        "instrumentationKeyMap": {
        "filters": [
            {
            "appFilter": ".*",
            "machineFilter": ".*",
            "virtualPathFilter": ".*",
            "instrumentationSettings" : {
                "connectionString": "$ConnectionString"
            }
            }
        ]
        }
    }
    }
"@

$privateCfgJsonString = '{}'
	
Set-AzVMExtension -ResourceGroupName $ResourceGroup -VMName $VMName -Location $Location -Name "ApplicationMonitoringWindows" -Publisher "Microsoft.Azure.Diagnostics" -Type "ApplicationMonitoringWindows" -Version "2.8" -SettingString $publicCfgJsonString -ProtectedSettingString $privateCfgJsonString

注意

对于更复杂的大规模部署,你可以使用 PowerShell 循环跨多个 VM 安装或更新 Application Insights 代理扩展。

查询 Azure 虚拟机的 Application Insights 代理扩展状态:

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

获取 Azure 虚拟机的已安装扩展的列表:

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

从 Azure 虚拟机卸载 Application Insights 代理扩展:

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

注意

通过选择与你在部署 Application Insights 代理扩展时使用的连接字符串相关联的 Application Insights 资源中的“实时指标流”来验证安装。 如果你从多个虚拟机发送数据,请在“服务器名称”下选择目标 Azure 虚拟机。 最长可能需要等待一分钟,数据才会开始流动。

为虚拟机规模集启用监视

可以使用 Azure 门户或 PowerShell 为虚拟机规模集启用监视。

Azure 门户

按照前面的 VM 步骤操作,但前往你的虚拟机规模集而不是 VM。

PowerShell

安装或更新用作虚拟机规模集扩展的 Application Insights 代理:

# Set resource group, vmss name, and connection string to reflect your environment
$ResourceGroup = "<myVmResourceGroup>"
$VMSSName = "<myVmName>"
$ConnectionString = "<myAppInsightsResourceConnectionString>"
$publicCfgHashtable =
@{
  "redfieldConfiguration"= @{
    "instrumentationKeyMap"= @{
      "filters"= @(
        @{
          "appFilter"= ".*";
          "machineFilter"= ".*";
          "virtualPathFilter"= ".*";
          "instrumentationSettings" = @{
            "connectionString"= "$ConnectionString"
          }
        }
      )
    }
  }
};
$privateCfgHashtable = @{};
$vmss = Get-AzVmss -ResourceGroupName $ResourceGroup -VMScaleSetName $VMSSName
Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "ApplicationMonitoringWindows" -Publisher "Microsoft.Azure.Diagnostics" -Type "ApplicationMonitoringWindows" -TypeHandlerVersion "2.8" -Setting $publicCfgHashtable -ProtectedSetting $privateCfgHashtable
Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -Name $vmss
# Note: Depending on your update policy, you might need to run Update-AzVmssInstance for each instance

获取虚拟机规模集的已安装扩展列表:

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

从虚拟机规模集中卸载应用程序监视扩展:

# set resource group and vmss name to reflect your environment
$vmss = Get-AzVmss -ResourceGroupName "<myResourceGroup>" -VMScaleSetName "<myVmssName>"
Remove-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "ApplicationMonitoringWindows"
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 虚拟机和虚拟机规模集上运行的 .NET 应用程序的 Application Insights 监视代理扩展的故障排除提示。

如果在部署扩展时遇到问题,请查看记录到以下目录中的文件的执行输出:

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

如果扩展已成功部署,但你看不到遥测数据,则可能发生了代理故障排除中介绍的以下问题之一:

  • 应用的 bin 目录中存在冲突的 Dll
  • 与 IIS 共享配置冲突

测试应用程序主机与引入服务之间的连接性

Application Insights SDK 和代理发送遥测,将其作为 REST 调用引入到引入终结点。 可以使用原始 REST 客户端通过 PowerShell 或使用 curl 命令,测试从 Web 服务器或应用程序主机计算机到引入服务终结点的连接。 请参阅排查 Azure Monitor Application Insights 中缺失应用程序遥测的问题

发行说明

2.8.44

  • 已将 Application Insights .NET/.NET Core SDK 更新到 2.20.1 - red field。
  • 启用了 SQL 查询集合。
  • 已启用对 Microsoft Entra 身份验证的支持。

2.8.42

已将 Application Insights .NET/.NET Core SDK 更新到 2.18.1 - 红色字段。

2.8.41

添加了 ASP.NET Core 自动检测功能。

后续步骤