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

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

注意

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

在本文中,你将了解如何通过三种不同的方法在 Azure 虚拟机 (VM) 或 Azure 虚拟机规模集上运行适用于 .NET 的 Application Insights Profiler:

  • Visual Studio 和 Azure 资源管理器
  • PowerShell
  • Azure 资源浏览器

选择首选方法选项卡以:

本指南介绍如何:

  • 配置 Azure 诊断扩展以运行 .NET Profiler。
  • 将 Application Insights SDK 安装到 VM 上。
  • 部署应用程序。
  • 通过 Azure 门户中的 Application Insights 实例查看 Profiler 跟踪。

注意

目前,本地服务器不支持适用于 .NET 的 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. 选择“更新”。

    屏幕截图显示在何处选择要更新的 Application Insights 包。

启用 .NET Profiler

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

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

安装 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. 部署应用程序。

后续步骤