您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

使用 Application Insights 探查实时 Azure Web 应用Profile live Azure web apps with Application Insights

Azure Application Insights 的此功能已分别面向 Azure 应用服务的 Web 应用功能和 Azure 计算资源提供正式版和预览版。This feature of Azure Application Insights is generally available for the Web Apps feature of Azure App Service and is in preview for Azure compute resources.

本文介绍了使用 Application Insights 时在实时 Web 应用程序的每个方法上花费的时间。This article discusses the amount of time that's spent in each method of your live web application when you use Application Insights. Application Insights Profiler 工具显示了由应用处理的实时请求的详细配置文件。The Application Insights Profiler tool displays detailed profiles of live requests that were served by your app. Profiler 突出显示使用时间最多的热路径。Profiler highlights the hot path that uses the most time. 将会根据采样分析响应时间各不相同的请求。Requests with various response times are profiled on a sampling basis. 可以通过各种技术尽量降低与应用程序相关联的开销。By using a variety of techniques, you can minimize the overhead that's associated with the application.

Profiler 目前适用于 Web 应用上运行的 ASP.NET 和 ASP.NET Core Web 应用。Profiler currently works for ASP.NET and ASP.NET Core web apps that are running on Web Apps. 若要使用 Profiler,“基本”服务层或更高层是必需的。The Basic service tier or higher is required to use Profiler.

为 Web Apps Web 应用启用 ProfilerEnable Profiler for your Web Apps web app

如果已将应用程序发布到 Web 应用,但尚未在源代码中执行使用 Application Insights 所需的任何操作,请执行以下操作:If you already have the application published to a web app, but have not done anything in the source code to use Application Insights, do the following:

  1. 转到 Azure 门户中的“应用服务”窗格。Go to the App Services pane in the Azure portal.
  2. 在“监视”下选择“Application Insights”,然后按窗格中的说明创建新资源,或者选择现有的 Application Insights 资源,以便监视 Web 应用。Under Monitoring, select Application Insights, and then either follow the instructions on the pane to create a new resource or select an existing Application Insights resource to monitor your web app.

    在应用服务门户上启用 App Insights

  3. 如果可以访问项目源代码,请安装 Application InsightsIf you have access to your project source code, install Application Insights.
    如果已安装,请确保使用的是最新版本。If it's already installed, make sure you have the latest version. 要检查最新版本,请在解决方案资源管理器中右键单击你的项目,然后选择“管理 NuGet 包” > “更新” > “更新所有包”。To check for the latest version, in Solution Explorer, right-click your project, and then select Manage NuGet packages > Updates > Update all packages. 然后部署应用。Then, deploy your app.

ASP.NET Core 应用程序需要安装 Microsoft.ApplicationInsights.AspNetCore NuGet 包 2.1.0-beta6 或更高版本才能使用 Profiler。ASP.NET Core applications require the installation of the Microsoft.ApplicationInsights.AspNetCore NuGet package 2.1.0-beta6 or later to work with Profiler. 从 2017 年 6 月 27 日开始,不支持更早的版本。As of June 27, 2017, earlier versions are not supported.

  1. Azure 门户中,打开 Web 应用的 Application Insights 资源。In the Azure portal, open the Application Insights resource for your web app.
  2. 选择“性能” > “启用 Application Insights Profiler”。Select Performance > Enable Application Insights Profiler.

    选择“启用探查器”横幅

  3. 另外,可以通过选择“Profiler”配置来查看状态,启用或禁用 Profiler。Alternatively, you can select the Profiler configuration to view the status and enable or disable Profiler.

    ASP.NET Profiler 配置

    配置有 Application Insights 的 Web 应用会在“探查器”配置窗格中列出。Web apps that are configured with Application Insights are listed in the Profiler configuration pane. 如果遵循了上述步骤,则应该已安装 Profiler 代理。If you followed the preceding steps, the Profiler agent should be installed.

  4. 在“Profiler”配置窗格中选择“启用 Profiler”。In the Profiler configuration pane, select Enable Profiler.

  5. 如果需要,请根据说明安装 Profiler 代理。If necessary, follow the instructions to install the Profiler agent. 如果尚无 Web 应用配置有 Application Insights,请选择“添加链接应用”。If no web apps have been configured with Application Insights, select Add Linked Apps.

    “配置”窗格选项

与通过 Web 应用计划托管的 Web 应用不同,在 Azure 计算资源(例如:Azure 虚拟机、虚拟机规模集、Azure Service Fabric 或 Azure 云服务)中托管的应用程序不由 Azure 直接管理。Unlike web apps that are hosted through Web Apps plans, applications that are hosted in Azure compute resources (for example, Azure Virtual Machines, virtual machine scale sets, Azure Service Fabric, or Azure Cloud Services) are not directly managed by Azure. 在这种情况下,将没有任何要链接到的 Web 应用。In this case, there's no web app to link to. 请改为选择“启用探查器”按钮,而不是链接到应用。Instead of linking to an app, select the Enable Profiler button.

为 Azure 计算资源启用 Profiler(预览)Enable Profiler for Azure compute resources (preview)

有关信息,请参阅用于 Azure 计算资源的 Profiler 的预览版本For information, see the preview version of Profiler for Azure compute resources.

查看探查器数据View profiler data

确保应用程序可以接收流量。Make sure your application is receiving traffic. 如果正在进行试验,可以通过 Application Insights 性能测试生成针对 Web 应用的请求。If you are doing an experiment, you can generate requests to your web app using Application Insights Performance Testing. 如果最近启用了 Profiler,则可以运行简短的负载测试大约 15 分钟,这样就会生成探查器跟踪。If you have newly enabled Profiler, you can run a short load test for about 15 minutes, which should generate profiler traces. 如果已启用 Profiler 相当长一段时间,请记住,Profiler 每小时会随机运行两次,每次运行两分钟。If you have had Profiler enabled for a while already, keep in mind that Profiler runs randomly two times every hour and for a duration of two minutes each time it runs. 建议运行负载测试一个小时,确保获取探查器跟踪样本。We recommend first running the load test for one hour to make sure you get sample profiler traces.

应用程序收到一些流量后,请转到“性能”窗格,选择“采取措施”,以查看 Profiler 跟踪,然后选择“Profiler 跟踪”按钮。After your application receives some traffic, go to the Performance pane, select Take Actions to view profiler traces, and then select the Profiler Traces button.

“Application Insights 性能”窗格预览 Profiler 跟踪

选择一个样本可以显示执行该请求所花费时间的代码级细节。Select a sample to display a code-level breakdown of time spent executing the request.

Application Insights 跟踪浏览器

跟踪浏览器会显示以下信息:The trace explorer displays the following information:

  • 显示热路径:打开最大的叶节点,或者至少打开某个接近的节点。Show Hot Path: Opens the biggest leaf node, or at least something close. 大多数情况下,此节点与性能瓶颈相邻。In most cases, this node is adjacent to a performance bottleneck.
  • 标签:函数或事件的名称。Label: The name of the function or event. 树中显示了代码与所发生事件(如 SQL 和 HTTP 事件)的混合形式。The tree displays a mix of code and events that occurred (like SQL and HTTP events). 最前面的事件表示请求总持续时间。The top event represents the overall request duration.
  • 所用时间:从操作开始到操作结束之间的时间间隔。Elapsed: The time interval between the start of the operation and the end of the operation.
  • 时间:运行函数或事件的时间,此值相对于运行其他函数的时间。When: The time when the function or event was running in relation to other functions.

如何读取性能数据How to read performance data

Microsoft 服务探查器结合使用采样方法和检测来分析应用程序的性能。The Microsoft service profiler uses a combination of sampling methods and instrumentation to analyze the performance of your application. 当详细收集操作正在进行时,服务探查器将每隔一毫秒对每台计算机的 CPU 指令指针采样。When detailed collection is in progress, the service profiler samples the instruction pointer of each machine CPU every millisecond. 每个样本捕获当前正在执行的线程的完整调用堆栈。Each sample captures the complete call stack of the thread that is currently executing. 它从高级抽象化和低级抽象化两个角度,提供有关线程当前行为的有用详细信息。It gives detailed information about what that thread was doing, both at a high level and at a low level of abstraction. 服务探查器还会收集其他事件(包括上下文切换事件、任务并行库 (TPL) 事件和线程池事件),以跟踪活动相关性和因果关系。The service profiler also collects other events to track activity correlation and causality, including context switching events, Task Parallel Library (TPL) events, and thread pool events.

时间线视图中显示的调用堆栈是采样和检测的结果。The call stack that's displayed in the timeline view is the result of the sampling and instrumentation. 由于每个样本会捕获线程的整个调用堆栈,因此包含 Microsoft .NET Framework 中的代码,以及引用的其他框架中的代码。Because each sample captures the complete call stack of the thread, it includes code from Microsoft .NET Framework, and from other frameworks that you reference.

对象分配(clr!JIT_New 或 clr!JIT_Newarr1)Object allocation (clr!JIT_New or clr!JIT_Newarr1)

clr!JIT_New 和 clr!JIT_Newarr1 是 .NET Framework 中的 helper 函数,用于分配托管堆中的内存。clr!JIT_New and clr!JIT_Newarr1 are helper functions in the .NET Framework that allocate memory from a managed heap. 分配对象时,将调用 clr!JIT_New。clr!JIT_New is invoked when an object is allocated. 分配对象数组时,将调用 clr!JIT_Newarr1。clr!JIT_Newarr1 is invoked when an object array is allocated. 这两个函数通常速度很快,花费的时间相对较短。These two functions are usually fast and take relatively small amounts of time. 如果发现时间线中的 clr!JIT_New 或 clr!JIT_Newarr1 花费了很长时间,则可能表示代码分配了很多对象,从而消耗了大量的内存。If you see clr!JIT_New or clr!JIT_Newarr1 take a substantial amount of time in your timeline, it indicates that the code might be allocating many objects and consuming significant amounts of memory.

加载代码 (clr!ThePreStub)Loading code (clr!ThePreStub)

clr!ThePreStub 是 .NET Framework 中的 helper 函数,用于准备首次执行的代码。clr!ThePreStub is a helper function in the .NET Framework that prepares the code to execute for the first time. 这通常包括但不限于恰时 (JIT) 编译。This usually includes, but is not limited to, just-in-time (JIT) compilation. 对于每个 C# 方法,在进程的生存期内,最多只应调用 clr!ThePreStub 一次。For each C# method, clr!ThePreStub should be invoked at most once during the lifetime of a process.

如果 clr!ThePreStub 针对某个请求花费了很长时间,则表示这是第一个执行该方法的请求。If clr!ThePreStub takes a substantial amount of time for a request, this indicates that the request is the first one that executes that method. .NET Framework 运行时加载该方法的时间非常重要。The time for the .NET Framework runtime to load the first method is significant. 可以考虑在用户访问该代码部分之前使用执行该代码部分的预热进程,或者考虑在程序集中运行本机映像生成器 (ngen.exe)。You might consider using a warmup process that executes that portion of the code before your users access it, or consider running Native Image Generator (ngen.exe) on your assemblies.

锁争用(clr!JITutil_MonContention 或 clr!JITutil_MonEnterWorker)Lock contention (clr!JITutil_MonContention or clr!JITutil_MonEnterWorker)

clr!JITutil_MonContention 或 clr!JITutil_MonEnterWorker 指示当前线程正在等待释放锁。clr!JITutil_MonContention or clr!JITutil_MonEnterWorker indicates that the current thread is waiting for a lock to be released. 执行 C# LOCK 语句、调用 Monitor.Enter 方法或者结合 MethodImplOptions.Synchronized 属性调用某个方法时,通常会显示此文本。This text is usually displayed when you execute a C# LOCK statement, when invoking the Monitor.Enter method, or when invoking a method with the MethodImplOptions.Synchronized attribute. 如果线程 A 获取了某个锁,而线程 B 在线程 A 释放该锁之前尝试获取同一个锁,此时通常会发生锁争用。Lock contention usually occurs when thread A acquires a lock, and thread B tries to acquire the same lock before thread A releases it.

加载代码 ([COLD])Loading code ([COLD])

如果方法名称包含 [COLD](例如 mscorlib.ni![COLD]System.Reflection.CustomAttribute.IsDefined),则表示 .NET Framework 运行时首次执行的代码未经过按配置优化功能的优化。If the method name contains [COLD], such as mscorlib.ni![COLD]System.Reflection.CustomAttribute.IsDefined, the .NET Framework runtime is executing code for the first time that is not optimized by profile-guided optimization. 对于每个方法,在进程的生存期内,它最多只应显示一次。For each method, it should be displayed at most once during the lifetime of the process.

如果针对某个请求加载代码花费的时间很长,这表示这是第一个执行该方法的未优化部分的请求。If loading code takes a substantial amount of time for a request, this indicates that the request is the first one to execute the unoptimized portion of the method. 请考虑在用户访问该代码部分之前使用执行该代码部分的预热进程。Consider using a warmup process that executes that portion of the code before your users access it.

发送 HTTP 请求Send HTTP request

HttpClient.Send 等方法指示代码正在等待某个 HTTP 请求完成。Methods like HttpClient.Send indicate that the code is waiting for an HTTP request to be completed.

数据库操作Database operation

SqlCommand.Execute 等方法指示代码正在等待某个数据库操作完成。Methods like SqlCommand.Execute indicate that the code is waiting for a database operation to finish.

等待 (AWAIT_TIME)Waiting (AWAIT_TIME)

AWAIT_TIME 指示代码正在等待另一个任务完成。AWAIT_TIME indicates that the code is waiting for another task to finish. 这种情况通常发生在 C# AWAIT 语句上。This usually happens with the C# AWAIT statement. 当代码执行 C# AWAIT 时,线程会回退并将控制权返回给线程池,此时,不会有任何阻塞的线程等待 AWAIT 完成。When the code does a C# AWAIT, the thread unwinds and returns control to the thread pool, and there is no thread that is blocked waiting for the AWAIT to finish. 但是,从逻辑上讲,执行 AWAIT 的线程会被“阻塞”并等待该操作完成。However, logically, the thread that did the AWAIT is "blocked," and is waiting for the operation to finish. AWAIT_TIME 语句指示等待任务完成的阻塞时间。The AWAIT_TIME statement indicates the blocked time waiting for the task to finish.

阻塞时间Blocked time

BLOCKED_TIME 指示代码正在等待另一个资源变为可用。BLOCKED_TIME indicates that the code is waiting for another resource to be available. 例如,它可能会等待同步对象或线程变为可用,或等待请求完成。For example, it might be waiting for a synchronization object, for a thread to be available, or for a request to finish.

CPU 时间CPU time

CPU 正忙于执行指令。The CPU is busy executing the instructions.

磁盘时间Disk time

应用程序正在执行磁盘操作。The application is performing disk operations.

网络时间Network time

应用程序正在执行网络操作。The application is performing network operations.

“时间”列When column

“时间”列是针对节点收集的非独占样本在各个时间发生的变化的可视化效果。The When column is a visualization of how the INCLUSIVE samples collected for a node vary over time. 请求的总范围划分成 32 个时间存储桶。The total range of the request is divided into 32 time buckets. 该节点的非独占样本会在这 32 个存储桶中累积。The inclusive samples for that node are accumulated in those 32 buckets. 每个存储桶用一个条形表示。Each bucket is represented as a bar. 条形的高度表示缩放后的值。The height of the bar represents a scaled value. 如果节点带有 CPU_TIME 或 BLOCKED_TIME 标记,或者跟资源(例如,CPU、磁盘、线程)的消耗存在某种明显关系,则条形表示在该 Bucket 的时间段内消耗了其中的某个资源。For nodes that are marked CPU_TIME or BLOCKED_TIME, or where there is an obvious relationship to consuming a resource (for example, a CPU, disk, or thread), the bar represents the consumption of one of the resources during the period of that bucket. 如果消耗多个资源,这些指标的值可能大于 100%。For these metrics, it's possible to get a value of greater than 100 percent by consuming multiple resources. 例如,如果在某个时间间隔内平均使用两个 CPU,则指标值为 200%。For example, if you use, on average, two CPUs during an interval, you get 200 percent.

限制Limitations

默认数据保留期为 5 天。The default data retention period is five days. 每天引入的最大数据量为 10 GB。The maximum data that's ingested per day is 10 GB.

使用 Profiler 服务没有任何费用。There are no charges for using the Profiler service. 若要使用 Profiler 服务,Web 应用必须至少托管在 Web 应用的“基本”层中。For you to use the Profiler service, your web app must be hosted in at least the Basic tier of Web Apps.

开销和采样算法Overhead and sampling algorithm

在托管的应用程序已启用 Profiler 来捕获跟踪的每个虚拟机上,Profiler 每小时随机运行 2 分钟。Profiler randomly runs two minutes every hour on each virtual machine that hosts the application that has Profiler enabled for capturing traces. Profiler 在运行时会给服务器增添 5% 到 15% 的 CPU 开销。When Profiler is running, it adds from 5 percent to 15 percent CPU overhead to the server.

可用于托管应用程序的服务器越多,Profiler 对总体应用程序性能的影响越小。The more servers that are available for hosting the application, the less impact Profiler has on the overall application performance. 这是因为采样算法会使 Profiler 在任何时候都只在 5% 的服务器上运行。This is because the sampling algorithm results in Profiler running on only 5 percent of servers at any time. 有更多服务器可用于处理 Web 请求,以抵消运行 Profiler 所产生的服务器开销。More servers are available to serve web requests to offset the server overhead caused by running Profiler.

禁用 ProfilerDisable Profiler

若要为单个 Web 应用实例停止或重启 Profiler,请在“Web 作业”下转到“Web 应用资源”。To stop or restart Profiler for an individual web apps instance, under Web Jobs, go to the Web Apps resource. 若要删除 Profiler,请转到“扩展”。To delete Profiler, go to Extensions.

禁用针对 Web 作业的 Profiler

建议在所有 Web 应用上都启用 Profiler,以便尽早发现任何性能问题。We recommend that you have Profiler enabled on all your web apps to discover any performance issues as early as possible.

如果使用 WebDeploy 将更改部署到 Web 应用程序,请确保排除 App_Data 文件夹,以防在部署期间将它删除。If you use WebDeploy to deploy changes to your web application, ensure that you exclude the App_Data folder from being deleted during deployment. 否则,下一次将 Web 应用程序部署到 Azure 时,会删除 Profiler 扩展文件。Otherwise, the Profiler extension's files are deleted the next time you deploy the web application to Azure.

故障排除Troubleshooting

活动分析会话太多Too many active profiling sessions

目前,最多可以对同一服务计划中运行的 4 个 Azure Web 应用和部署槽启用 Profiler。Currently, you can enable Profiler on a maximum of four Azure web apps and deployment slots that are running in the same service plan. 如果 Profiler Web 作业报告活动探查会话太多,请将一些 Web 应用移到另一服务计划。If the Profiler web job is reporting too many active profiling sessions, move some web apps to a different service plan.

如何确定 Application Insights Profiler 是否正在运行?How do I determine whether Application Insights Profiler is running?

Profiler 在 Web 应用中以连续 Web 作业的形式运行。Profiler runs as a continuous web job in the web app. 可以在 Azure 门户中打开 Web 应用资源。You can open the web app resource in the Azure portal. 在“WebJobs”窗格中,查看 ApplicationInsightsProfiler 的状态。In the WebJobs pane, check the status of ApplicationInsightsProfiler. 如果探查器未运行,请打开“日志”获取详细信息。If it isn't running, open Logs to get more information.

为何运行 Profiler 后仍找不到任何堆栈示例?Why can't I find any stack examples, even though Profiler is running?

下面是可以检查的几个因素:Here are a few things that you can check:

  • 确保 Web 应用服务计划属于“基本”层或更高层。Make sure that your web app service plan is Basic tier or higher.
  • 确保 Web 应用中已启用 Application Insights SDK 2.2 Beta 或更高版本。Make sure that your web app has Application Insights SDK 2.2 Beta or later enabled.
  • 确保配置 Web 应用中的 APPINSIGHTS_INSTRUMENTATIONKEY 设置所采用的检测密钥与 Application Insights SDK 使用的检测密钥相同。Make sure that your web app has the APPINSIGHTS_INSTRUMENTATIONKEY setting configured with the same instrumentation key that's used by the Application Insights SDK.
  • 确保 Web 应用在 .NET Framework 4.6 上运行。Make sure that your web app is running on .NET Framework 4.6.
  • 如果 Web 应用是一个 ASP.NET Core 应用程序,请检查所需的依赖项If your web app is an ASP.NET Core application, check the required dependencies.

启动 Profiler 后,将有短暂的一段预热时间,在此期间,Profiler 会积极收集多种性能跟踪。After Profiler is started, there is a short warmup period during which Profiler actively collects several performance traces. 此后,Profiler 会每隔一小时收集性能跟踪两分钟。After that, Profiler collects performance traces for two minutes every hour.

使用 Azure 服务探查器时出现问题,I was using Azure Service profiler. 原因是什么?What happened to it?

启用 Application Insights Profiler 时,会禁用 Azure 服务探查器代理。When you enable Application Insights Profiler, the Azure Service profiler agent is disabled.

并行线程的重复计算Double counting in parallel threads

在某些情况下,堆栈查看器中的总时间指标大于请求的持续时间。In some cases, the total time metric in the stack viewer is more than the duration of the request.

如果存在与请求关联的两个或更多个线程并且它们正并行运行,则可能会发生这种情况。This situation might occur when two or more threads are associated with a request, and they are operating in parallel. 在这种情况下,总线程时间就会超过已用时间。In that case, the total thread time is more than the elapsed time. 一个线程可能会等待另一个线程完成。One thread might be waiting on the other to be completed. 查看器会尝试检测此数据并省略不相关的等待时间,但是,它会倾向于显示过多信息,而不是省略关键信息。The viewer tries to detect this and omits the uninteresting wait, but it errs on the side of displaying too much information rather than omit what might be critical information.

如果看到跟踪中出现并行线程,请确定哪些线程处于等待状态,以便可以确定请求的关键路径。When you see parallel threads in your traces, determine which threads are waiting so you can ascertain the critical path for the request. 在大多数情况下,快速进入等待状态的线程只是在等待其他线程完成。In most cases, the thread that quickly goes into a wait state is simply waiting on the other threads. 请专注于其他线程,忽略等待中线程花费的时间。Concentrate on the other threads, and ignore the time in the waiting threads.

无探查数据No profiling data

下面是可以检查的几个因素:Here are a few things that you can check:

  • 如果要查看的数据的期限超过了好几周,请尝试限制时间筛选器并重试。If the data you are trying to view is older than a couple of weeks, try limiting your time filter and try again.
  • 确保代理或防火墙未阻止对 https://gateway.azureserviceprofiler.net 的访问。Ensure that proxies or a firewall have not blocked access to https://gateway.azureserviceprofiler.net.
  • 确保在应用中使用的 Application Insights 检测密钥与启用探查所用的 Application Insights 资源使用的检测密钥相同。Ensure that the Application Insights instrumentation key you are using in your app is the same as the Application Insights resource that you used to enabled profiling. 密钥通常在 ApplicationInsights.config 文件中,但也可能在 web.config 或 app.config 文件中。The key is usually in the ApplicationInsights.config file, but it might also be in the web.config or app.config file.

探查查看器中的错误报告Error report in the profiling viewer

在门户中提交支持票证。Submit a support ticket in the portal. 请务必包含错误消息中的相关性 ID。Be sure to include the correlation ID from the error message.

部署错误:目录不为空“D:\home\site\wwwroot\App_Data\jobs”Deployment error: Directory Not Empty 'D:\home\site\wwwroot\App_Data\jobs'

如果在已启用 Profiler 的情况下将 Web 应用重新部署到 Web 应用资源,可能会看到如下消息:If you are redeploying your web app to a Web Apps resource with Profiler enabled, you might see a message like the following:

目录不为空“D:\home\site\wwwroot\App_Data\jobs”Directory Not Empty 'D:\home\site\wwwroot\App_Data\jobs'

如果通过脚本或通过 Visual Studio Team Services 部署管道运行 Web 部署,则会发生此错误。This error occurs if you run Web Deploy from scripts or from the Visual Studio Team Services Deployment Pipeline. 解决方法是将以下附加部署参数添加到 Web 部署任务:The solution is to add the following additional deployment parameters to the Web Deploy task:

-skip:Directory='.*\\App_Data\\jobs\\continuous\\ApplicationInsightsProfiler.*' -skip:skipAction=Delete,objectname='dirPath',absolutepath='.*\\App_Data\\jobs\\continuous$' -skip:skipAction=Delete,objectname='dirPath',absolutepath='.*\\App_Data\\jobs$'  -skip:skipAction=Delete,objectname='dirPath',absolutepath='.*\\App_Data$'

这些参数将删除 Application Insights Profiler 所用的文件夹,并取消阻止重新部署进程。These parameters delete the folder that's used by Application Insights Profiler and unblock the redeploy process. 它们不影响当前运行的 Profiler 实例。They don't affect the Profiler instance that's currently running.

手动安装Manual installation

配置 Profiler 时,将对 Web 应用的设置进行更新。When you configure Profiler, updates are made to the web app's settings. 如果你的环境有此要求,则可以手动应用更新。You can apply the updates manually if your environment requires it. 例如,应用程序在适用于 PowerApps 的 Web 应用环境中运行。An example might be that your application is running in a Web Apps environment for PowerApps.

  1. 在“Web 应用控制”窗格中,打开“设置”。In the Web App Control pane, open Settings.
  2. 将“.Net Framework 版本”设置为“v4.6”。Set .Net Framework version to v4.6.
  3. 将“Always On”设置为“打开”。Set Always On to On.
  4. 添加应用设置“APPINSIGHTS_INSTRUMENTATIONKEY”,将值设置为 SDK 使用的同一检测密钥。Add the APPINSIGHTS_INSTRUMENTATIONKEY app setting, and set the value to the same instrumentation key that's used by the SDK.
  5. 打开“高级工具”。Open Advanced Tools.
  6. 选择“转到”打开 Kudu 网站。Select Go to open the Kudu website.
  7. 在 Kudu 网站上,选择“站点扩展”。On the Kudu website, select Site extensions.
  8. 从 Azure Web 应用库中安装“Application Insights”。Install Application Insights from the Azure Web Apps Gallery.
  9. 重新启动 Web 应用。Restart the web app.

手动触发 ProfilerManually trigger Profiler

开发 Profiler 时,我们添加了一个命令行界面,以便测试应用服务中的 Profiler。When we developed Profiler, we added a command-line interface so that we could test Profiler on app services. 用户也可以使用此界面来自定义 Profiler 的启动方式。By using this same interface, users can also customize how Profiler starts. 在较高层面上,Profiler 使用 Web 应用 Kudu 系统在后台管理探查。At a high level, Profiler uses the Web Apps Kudu System to manage profiling in the background. 当你安装 Application Insights 扩展时,我们会创建一个用于托管 Profiler 的连续 Web 作业。When you install the Application Insights Extension, we create a continuous web job that hosts Profiler. 我们将使用与此相同的技术创建一个新的 Web 作业,你可以根据需要自定义此作业。We use this same technology to create a new web job that you can customize to fit your needs.

本部分介绍如何:This section explains how to:

  • 创建一个 Web 作业。只需按下某个按钮,该作业就能将 Profiler 启动两分钟。Create a web job, which can start Profiler for two minutes with the press of a button.
  • 创建一个可以计划运行 Profiler 的 Web 作业。Create a web job, which can schedule Profiler to run.
  • 设置 Profiler 的参数。Set arguments for Profiler.

设置Set up

首先,请自行熟悉 Web 作业的仪表板。First, familiarize yourself with the web job's dashboard. 在“设置”下选择“WebJobs”选项卡。Under Settings, select the WebJobs tab.

Web 作业边栏选项卡

可以看到,此仪表板显示了站点上当前安装的所有 Web 作业。As you can see, this dashboard displays all the web jobs that are currently installed on your site. 可以看到正在运行 Profiler 作业的 ApplicationInsightsProfiler2 Web 作业。You can see the ApplicationInsightsProfiler2 web job, which has the Profiler job running. 我们将在此位置为手动和计划探查创建新的 Web 作业。This is where we create new web jobs for manual and scheduled profiling.

若要获取所需的二进制文件,请执行以下操作:To get the binaries you need, do the following:

  1. 在 Kudu 站点的“开发工具”选项卡下面,选择带有 Kudu 徽标的“高级工具”选项卡,然后选择“转到”。On the Kudu site, on the Development tools tab, select the Advanced Tools tab with the Kudu logo, and then select Go.
    此时会打开新站点,你会自动登录。A new site opens, and you are signed in automatically.
  2. 若要下载 Profiler 二进制文件,请通过“调试控制台” > “CMD”转到位于页面顶部的文件资源管理器。To download the Profiler binaries, go to File Explorer via Debug Console > CMD, which is located at the top of the page.
  3. 选择“站点” > “wwwroot” > “App_Data” > “作业” > “连续”。Select Site > wwwroot > App_Data > Jobs > Continuous.
    此时会看到“ApplicationInsightsProfiler2”文件夹。You should see a folder named ApplicationInsightsProfiler2.
  4. 在文件夹左侧选择“下载”图标。At the left of the folder, select the Download icon.
    此操作下载 ApplicationInsightsProfiler2.zip 文件。This action downloads the ApplicationInsightsProfiler2.zip file. 建议创建一个空目录,以便将此 zip 存档移到该目录。We recommend that you create a clean directory to move this zip archive to.

设置 Web 作业存档Setting up the web job archive

实质上,将新的 Web 作业添加到 Azure 网站就是创建一个包含 run.cmd 文件的 zip 存档。When you add a new web job to the Azure website, you essentially create a zip archive with a run.cmd file inside. run.cmd 文件告知 Web 作业系统在你运行该 Web 作业时要执行哪些操作。The run.cmd file tells the web job system what to do when you run the web job.

  1. 创建新文件夹(例如,RunProfiler2Minutes)。Create a new folder (for example, RunProfiler2Minutes).
  2. 将提取的 ApplicationInsightProfiler2 文件夹中的文件复制到此新文件夹。Copy the files from the extracted ApplicationInsightProfiler2 folder into this new folder.
  3. 创建新的 run.cmd 文件。Create a new run.cmd file.
    为方便起见,可以在启动之前打开 Visual Studio Code 中的工作文件夹。For convenience, you can open the working folder in Visual Studio Code before you start.
  4. 在文件中添加命令 ApplicationInsightsProfiler.exe start --engine-mode immediate --single --immediate-profiling-duration 120In the file, add the command ApplicationInsightsProfiler.exe start --engine-mode immediate --single --immediate-profiling-duration 120. 这些命令如下所述:The commands are described as follows:

    • start:告知 Profiler 启动。start: Tells Profiler to start.
    • --engine-mode immediate:告知 Profiler 立即开始探查。--engine-mode immediate: Tells Profiler to begin profiling immediately.
    • --single:告知 Profiler 自动地先运行后停止。--single: Tells Profiler to run and then stop automatically.
    • --immediate-profiling-duration 120:告知 Profiler 运行 120 秒,即 2 分钟。--immediate-profiling-duration 120: Tells Profiler to run for 120 seconds, or 2 minutes.
  5. 保存所做更改。Save your changes.

  6. 将文件夹存档,方法是:右键单击该文件夹,然后选择“发送到” > “压缩的(zip 格式)文件夹”。Archive the folder by right-clicking it and then selecting Send to > Compressed (zipped) folder.
    此操作使用文件夹的名称创建一个 .zip 文件。This action creates a .zip file that uses the name of your folder.

启动 Profiler 命令

现在已创建 Web 作业 .zip 文件,该文件可以用来在站点中设置 Web 作业。You have now created a web job .zip file, which you can use to set up web jobs in your site.

添加新的 Web 作业Add a new web job

此部分在站点中添加新的 Web 作业。In this section, you add a new web job on your site. 以下示例演示如何添加手动触发的 Web 作业。The following example shows how to add a manually triggered web job. 添加手动触发的 Web 作业以后,过程与计划内 Web 作业几乎相同。After you've added the manually triggered web job, the process is nearly the same for a scheduled web job.

  1. 转到“Web 作业”仪表板。Go to the Web jobs dashboard.
  2. 在工具栏中选择“添加”。On the toolbar, select Add.
  3. 为 Web 作业提供一个名称。Give your web job a name.
    具体说来,它可以用来匹配存档的名称并将其打开,不管 run.cmd 文件有多少不同的版本。For clarity, it can help to match the name of your archive and to open it up for a variety of versions of the run.cmd file.
  4. 在窗体的“文件上传”区域,选择“打开文件”图标,然后搜索在上一部分创建的 .zip 文件。In the File upload area of the form, select the Open file icon, and then search for the .zip file that you created in the preceding section.

    a.a. 在“类型”框中,选择“已触发”。In the Type box, select Triggered.
    b.b. 在“触发器”框中,选择“手动”。In the Triggers box, select Manual.

  5. 选择“确定”。Select OK.

启动 Profiler 命令

运行 ProfilerRun Profiler

有了可以手动触发的新 Web 作业以后,即可尝试按此部分的说明运行它。Now that you have a new web job that you can trigger manually, you can try to run it by following the instructions in this section.

根据设计,在任意给定时间,计算机上只能运行一个 ApplicationInsightsProfiler.exe 进程。By design, you can have only one ApplicationInsightsProfiler.exe process running on a machine at any given time. 因此,在开始之前,请务必在此仪表板中禁用“连续”Web 作业。So, before you begin, disable the Continuous web job from this dashboard.

  1. 选择新 Web 作业所在的行,然后选择“停止”。Select the row with the new web job, and then select Stop.
  2. 在工具栏上选择“刷新”,并确认状态是否显示作业已停止。On the toolbar, select Refresh, and confirm that the status indicates that the job is stopped.
  3. 选择新 Web 作业所在的行,然后选择“运行”。Select the row with the new web job, and then select Run.
  4. 在行仍处于选中状态的情况下,在工具栏中选择“日志”命令。With the row still selected, on the toolbar, select the Logs command.
    此操作为新的 Web 作业打开 Web 作业仪表板,该仪表板会列出最新的运行及其结果。This action opens a web jobs dashboard for the new web job, and it lists the most recent runs and their results.
  5. 选择刚刚启动的运行实例。Select the instance of the run you've just started.
    如果已成功触发新的 Web 作业,可以查看一些来自 Profiler 的诊断日志,这些日志确认探查已启动。If you've successfully triggered the new web job, you can view some diagnostic logs coming from Profiler that confirm that the profiling has started.

注意事项Things to consider

尽管此方法相对简单直接,但需要注意以下问题:Though this method is relatively straightforward, consider the following:

  • 由于 Web 作业不是由我们的服务进行管理,因此,我们无法更新 Web 作业的代理二进制文件。Because your web job is not managed by our service, we have no way to update the agent binaries for your web job. 目前,二进制文件没有稳定的下载页,因此获取最新二进制文件的唯一方法是更新扩展,然后从 Continuous 文件夹中抓取文件,就像前面所做的一样。We do not currently have a stable download page for our binaries, so the only way to get the latest binaries is by updating your extension and grabbing it from the Continuous folder as you did in the previous steps.

  • 由于此过程使用的命令行参数本来是为开发人员而不是最终用户设计的,因此这些参数未来可能会变化。Because this process utilizes command-line arguments that were originally designed for developers rather than end-users, the arguments might change in the future. 升级时,请注意可能会有的变化。Be aware of possible changes when you upgrade. 这不会造成太大的问题,因为可以添加 Web 作业,运行它,然后进行测试,确保它正常工作。It shouldn't be much of a problem, because you can add a web job, run it, and test to ensure that it works. 最终我们会生成一个 UI 来处理这种情况,不使用手动过程。Eventually, we will build a UI to handle this without the manual process.

  • Web 应用的 Web 作业功能是独特的。The Web Jobs feature of Web Apps is unique. 当它运行 Web 作业时,可以确保进程使用的环境变量和应用设置,与网站将使用的环境变量和应用设置相同。When it runs the web job, it ensures that your process has the same environment variables and app settings that your website will have. 这意味着,不需要通过命令行将检测密钥传递给 Profiler。This means that you do not need to pass the instrumentation key through the command line to Profiler. Profiler 会从环境中拾取检测密钥。Profiler should pick up the instrumentation key from the environment. 但是,如果想要在 Web 应用外部的开发机箱或计算机上运行 Profiler,则需要提供检测密钥。However, if you want to run Profiler on your dev box or on a machine outside of Web Apps, you need to supply an instrumentation key. 为此,可以传入参数 --ikey <instrumentation-key>You can do so by passing an argument, --ikey <instrumentation-key>. 请注意,此值必须与应用程序正使用的检测密钥匹配。This value must match the instrumentation key that your application is using. Profiler 的日志输出会告知你 Profiler 是使用哪个 ikey 启动的,以及在探查时,我们是否通过该检测密钥检测到活动。The log output from Profiler tells you which ikey Profiler started with and whether we detected activity from that instrumentation key while we were profiling.

  • 手动触发的 Web 作业可以通过 Web 挂钩来触发。Manually triggered web jobs can be triggered via Web Hook. 可以右键单击仪表板上的 Web 作业,然后查看属性来获取此 URL。You can get this URL by right-clicking the web job on the dashboard and viewing the properties. 也可以先在表中选择 Web 作业,然后在工具栏中选择“属性”。Or, in the toolbar, you can select Properties after you select the web job in the table. 此方法会开启无限的可能,例如从 CI/CD 管道(例如 VSTS)或 Microsoft Flow (https://flow.microsoft.com/en-us/) 之类的应用程序触发 Profiler。This approach opens up endless possibilities, such as triggering Profiler from your CI/CD pipeline (like VSTS) or something like Microsoft Flow (https://flow.microsoft.com/en-us/). 从根本上讲,具体选择取决于你想要使 run.cmd 文件(也可以是 run.ps1 文件)达到多复杂的程度,但不管怎样,你可以很灵活地进行操作。Ultimately, your choice depends on how complex you want to make your run.cmd file (which can also be a run.ps1 file), but the flexibility is there.

后续步骤Next steps