为 Azure 虚拟机上的 Web 应用启用 Profiler

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

本文介绍如何通过三种不同的方法在 Azure 虚拟机 (VM) 或 Azure 虚拟机规模集上运行 Application Insights Profiler:

  • Visual Studio 和 Azure 资源管理器

  • 通过以下任一方法使用 PowerShell,可以:

  • 配置 Azure 诊断扩展以运行 Profiler。

  • 将 Application Insights SDK 安装到 VM 上。

  • 部署应用程序。

  • 通过 Azure 门户中的 Application Insights 实例查看 Profiler 跟踪。

先决条件

将 Application Insights SDK 添加到应用程序

  1. 在 Visual Studio 中打开 ASP.NET Core 项目。

  2. 选择“项目”>“添加 Application Insights 遥测” 。

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

  4. 选择 Application Insights 资源所在的订阅,然后选择“下一步”。

  5. 选择要将连接字符串保存到的位置,然后选择“下一步”。

  6. 选择“完成”。

注意

有关完整说明,包括在没有 Visual Studio 的情况下如何在 ASP.NET Core 应用程序上启用 Application Insights,请参阅适用于 ASP.NET Core 应用程序的 Application Insights

升级到最新稳定版本的 Application Insights SDK

  1. 转到“项目”>“管理 NuGet 包”。

  2. 选择“Microsoft.ApplicationInsights.AspNetCore”。

  3. 在侧窗格中,从下拉列表中选择最新版本的 SDK。

  4. 选择更新

    Screenshot that shows where to select the Application Insights package for update.

启用探查器

可以通过以下两种方式之一启用 Profiler:

  • 在 ASP.NET Core 应用程序中使用 Azure 资源管理器模板和 Visual Studio。 我们建议使用此方法。
  • 通过 Azure CLI 使用 PowerShell 命令。

安装 Azure 诊断扩展

  1. 选择要使用的 ARM 模板:

  2. 在模板中,找到 extension 类型的资源。

  3. 在 Visual Studio 中,转到安装 Application Insights SDK 时添加的 ASP.NET Core 应用程序中的 arm.json 文件。

  4. 将模板中的资源类型 extension 添加到 arm.json 文件,以使用 Azure 诊断设置 VM 或虚拟机规模集。

  5. WadCfg 标记中,将 Application Insights 检测密钥添加到 MyApplicationInsightsProfilerSink

    "WadCfg": {
      "SinksConfig": {
        "Sink": [
          {
            "name": "MyApplicationInsightsProfilerSink",
            "ApplicationInsightsProfiler": "YOUR_APPLICATION_INSIGHTS_INSTRUMENTATION_KEY"
          }
        ]
      }
    }        
    
  6. 部署应用程序。

Profiler 是否可以在本地服务器上运行?

目前,本地服务器不支持 Application Insights Profiler。

后续步骤