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

为 Azure 应用服务应用启用 Profiler

Application Insights Profiler 是作为 Azure 应用服务运行时的一部分预先安装的。 通过使用基本服务层或更高服务层,可以在运行在应用服务上的 ASP.NET 和 ASP.NET Core 应用上运行 Profiler。 即使在生成时已在应用程序中包含了 Application Insights SDK,也应该执行这些步骤。

若要在 Linux 上启用 Profiler,请按 ASP.NET Core Azure Linux Web 应用说明进行演练。

注意

Application Insights Profiler 的无代码安装遵循 .NET Core 支持策略。 若要详细了解支持的运行时,请参阅 .Net Core 支持策略

先决条件

验证是否已启用“始终可用”设置

  1. 在 Azure 门户中,转到应用服务实例。

  2. 在左侧窗格的“设置”下,选择“配置”。

    Screenshot that shows selecting Configuration on the left pane.

  3. 选择“常规设置”选项卡。

  4. 验证是否已选择“始终可用”>“”。

    Screenshot that shows the General tab on the Configuration pane showing that Always On is enabled.

  5. 如果进行了更改,请选择“保存”。

启用 Application Insights 和 Profiler

以下部分介绍如何为同一订阅或不同订阅启用 Application Insights。

对于同一订阅中的 Application Insights 和应用服务

如果 Application Insights 资源与应用服务的实例处于同一订阅中:

  1. 在左侧窗格的“设置”下,选择“Application Insights”。

    Screenshot that shows selecting Application Insights on the left pane.

  2. 在“Application Insights”下,选择“启用”。

  3. 验证是否已将 Application Insights 资源连接到应用。

    Screenshot that shows enabling Application Insights on your app.

  4. 向下滚动并选择“.NET”或“.NET Core”选项卡,具体取决于应用。

  5. 验证是否选择了“收集级别”>“建议”。

  6. 在 Profiler 下,选择“开”。

    如果之前选择了“基本”收集级别,则会禁用 Profiler 设置。

  7. 选择“应用”>“”以进行确认。

    Screenshot that shows enabling Profiler on your app.

对于不同订阅中的 Application Insights 和应用服务

如果 Application Insights 资源与应用服务位于不同的订阅中,则需要通过为应用服务实例创建应用设置来手动启用 Profiler。 可以使用模板或其他方式自动创建这些设置。 以下是启用 Profiler 所需的设置。

应用设置
APPINSIGHTS_INSTRUMENTATIONKEY Application Insights 资源的 iKey
APPINSIGHTS_PROFILERFEATURE_VERSION 1.0.0
DiagnosticServices_EXTENSION_VERSION ~3

使用以下方法设置这些值:

为区域云启用 Profiler

目前唯一需要修改终结点的区域是 Azure 政府由世纪互联运行的 Microsoft Azure

应用设置 美国政府云 中国云
ApplicationInsightsProfilerEndpoint https://profiler.monitor.azure.us https://profiler.monitor.azure.cn
ApplicationInsightsEndpoint https://dc.applicationinsights.us https://dc.applicationinsights.azure.cn

为配置文件引入启用 Microsoft Entra 身份验证

Application Insights Profiler 支持对配置文件引入进行 Microsoft Entra 身份验证。 要引入应用程序的所有配置文件,你的应用程序必须经过身份验证并向 Profiler 代理提供所需的应用程序设置。

只有当你的应用程序中使用 Application Insights SDK 来引用和配置 Microsoft Entra ID 时,Profiler 才支持 Microsoft Entra 身份验证。

若要为配置文件引入启用 Microsoft Entra,请执行以下操作:

  1. 创建用于对 Application Insights 资源进行身份验证的托管标识并将其添加到应用服务:

    1. 系统分配的托管标识文档

    2. 用户分配的托管标识文档

  2. 在 Application Insights 资源中配置并启用 Microsoft Entra ID

  3. 添加以下应用程序设置,以告知 Profiler 代理使用哪个托管标识。

    • 对于系统分配的标识:

      应用设置
      APPLICATIONINSIGHTS_AUTHENTICATION_STRING Authorization=AAD
    • 对于用户分配的标识:

      应用设置
      APPLICATIONINSIGHTS_AUTHENTICATION_STRING Authorization=AAD;ClientId={Client id of the User-Assigned Identity}

禁用 Profiler

为单个应用实例停止或重启 Profiler:

  1. 在左侧窗格的“设置”下,选择“WebJobs”。

    Screenshot that shows selecting web jobs on the left pane.

  2. 选择名为 ApplicationInsightsProfiler3 的 Web 作业。

  3. 选择“停止” 。

    Screenshot that shows selecting stop for stopping the webjob.

  4. 请选择“是”以确认。

建议在所有应用上都启用 Profiler,以便尽早发现任何性能问题。

使用 WebDeploy 将更改部署到 Web 应用程序时,可以删除 Profiler 的文件。 可以通过在部署期间从要删除的内容中排除 App_Data 文件夹来防止删除该文件夹。

后续步骤