在运行时使用 Application Insights 无代码附加检测 Web 应用Instrument web apps at runtime with Application Insights Codeless Attach

重要

不再建议使用状态监视器,并且从 2021 年 6 月 1 日起将不再支持这个版本的状态监视器。Status Monitor is no longer recommended for use, and starting June 1st 2021 this version of Status monitor will not be supported. 它已被 Azure Monitor Application Insights 代理(以前称为状态监视器 v2)取代。It has been replaced by the Azure Monitor Application Insights Agent (formerly named Status Monitor v2). 若要了解本地服务器部署Azure 虚拟机和虚拟机规模集部署,请参阅我们的文档。See our documentation for on-premises server deployments or Azure virtual machine and virtual machine scale set deployments.

无需修改或重新部署代码,即可使用 Azure Application Insights 检测实时 Web 应用。You can instrument a live web app with Azure Application Insights, without having to modify or redeploy your code. 需要一个 Azure 订阅。You need a Azure subscription.

状态监视器可用于检测托管在 IIS、本地或 VM 中的 .NET 应用程序。Status Monitor is used to instrument a .NET application hosted in IIS either on-premises or in a VM.

  • 如果将应用部署到 Azure VM 或 Azure 虚拟机规模集,请按照这些说明操作。If your app is deployed into Azure VM or Azure virtual machine scale set, follow these instructions.
  • 如果应用部署在 Azure 应用服务中,请遵循这些说明If your app is deployed into Azure app services, follow these instructions.
  • 如果应用部署在 Azure VM 中,则可通过 Azure 控制面板启用 Application Insights 监视。If your app is deployed in an Azure VM, you can switch on Application Insights monitoring from the Azure control panel.
  • (我们还单独提供了有关检测 Azure 云服务的文章。)(There are also separate articles about instrumenting Azure Cloud Services.)

包含失败请求、服务器响应时间和服务器请求信息的 App Insights 概览图屏幕截图

可以选择两种途径将 Application Insights 应用到 .NET Web 应用程序:You have a choice of two routes to apply Application Insights to your .NET web applications:

  • 生成时: 将 Application Insights SDK 添加到 Web 应用代码。Build time: Add the Application Insights SDK to your web app code.
  • 运行时: 如下所述检测服务器上的 Web 应用,无需重建并重新部署代码。Run time: Instrument your web app on the server, as described below, without rebuilding and redeploying the code.

备注

如果使用生成时检测,那么即使启用了运行时检测,它也无法正常工作。If you use build time instrumentation, run time instrumentation will not work even if it is turned on.

下面是每种途径的优势摘要:Here's a summary of what you get by each route:

构建时Build time 运行时Run time
请求和异常Requests & exceptions Yes Yes
更详细异常More detailed exceptions Yes
依赖项诊断Dependency diagnostics 在 NET 4.6+ 上,但更少详细信息On .NET 4.6+, but less detail 是,完整的详细信息:结果代码、SQL 命令文本、HTTP 谓词Yes, full detail: result codes, SQL command text, HTTP verb
系统性能计数器System performance counters Yes Yes
自定义遥测 APIAPI for custom telemetry Yes No
跟踪日志集成Trace log integration Yes No
页面视图和用户数据Page view & user data Yes No
需要重新生成代码Need to rebuild code Yes No

监视实时 IIS Web 应用Monitor a live IIS web app

如果应用托管在 IIS 服务器上,请使用状态监视器启用 Application Insights。If your app is hosted on an IIS server, enable Application Insights by using Status Monitor.

  1. 在 IIS Web 服务器上,使用管理员凭据登录。On your IIS web server, sign in with administrator credentials.

  2. 如果尚未安装 Application Insights 状态监视器,请下载并运行安装程序If Application Insights Status Monitor is not already installed, download and run the installer

  3. 在状态监视器中,选择已安装的 Web 应用程序或者要监视的网站。In Status Monitor, select the installed web application or website that you want to monitor. 使用 Azure 凭据登录。Sign in with your Azure credentials.

    配置资源,以便在其中通过 Application Insights 门户查看结果。Configure the resource where you want to see the results in the Application Insights portal. (通常情况下,最好是创建新的资源。(Normally, it's best to create a new resource. 如果已针对此应用进行了 Web 测试客户端监视,请选择现有资源。)Select an existing resource if you already have web tests or client monitoring for this app.)

    选择应用和资源。

  4. 重新启动 IIS。Restart IIS.

    选择对话框顶部的“重新启动”。

    Web 服务会中断片刻时间。Your web service is interrupted for a short while.

自定义监视选项Customize monitoring options

启用 Application Insights 就会将 DLL 和 ApplicationInsights.config 添加到 Web 应用。Enabling Application Insights adds DLLs and ApplicationInsights.config to your web app. 可以编辑 .config 文件,对某些选项进行更改。You can edit the .config file to change some of the options.

重新发布应用时,请重新启用 Application InsightsWhen you re-publish your app, re-enable Application Insights

重新发布应用之前,可考虑在 Visual Studio 中将 Application Insights 添加到代码Before you re-publish your app, consider adding Application Insights to the code in Visual Studio. 用户会获得更详细的遥测,并可编写自定义遥测。You'll get more detailed telemetry and the ability to write custom telemetry.

如果需要重新发布,但又不希望将 Application Insights 添加到代码中,则请注意,部署过程可能会从已发布网站中删除 DLL 和 ApplicationInsights.config。If you want to re-publish without adding Application Insights to the code, be aware that the deployment process may delete the DLLs and ApplicationInsights.config from the published web site. 因此:Therefore:

  1. 如果已编辑 ApplicationInsights.config,则请对其进行复制,再重新发布应用。If you edited ApplicationInsights.config, take a copy of it before you re-publish your app.
  2. 重新发布应用。Republish your app.
  3. 重新启用 Application Insights 监视。Re-enable Application Insights monitoring. (使用适当的方法:Azure Web 应用控制面板或 IIS 主机上的状态监视器。)(Use the appropriate method: either the Azure web app control panel, or the Status Monitor on an IIS host.)
  4. 恢复对 .config 文件所做的任何编辑。Reinstate any edits you performed on the .config file.

故障排除Troubleshooting

确认安装有效Confirm a valid installation

可执行以下步骤,确认已成功安装。These are some steps that you can perform to confirm that your installation was successful.

  • 确认 applicationInsights.config 文件在目标应用目录中并且包含 ikey。Confirm that the applicationInsights.config file is present in the target app directory and contains your ikey.

  • 如果怀疑缺失数据,可在 Analytics 中运行查询,列出目前正在发送遥测数据的所有云角色。If you suspect that data is missing, you can run a query in Analytics to list all the cloud roles currently sending telemetry.

    union * | summarize count() by cloud_RoleName, cloud_RoleInstance
    
  • 如果需要确认 Application Insights 已成功附加,可在命令窗口中运行 Sysinternals Handle,确认 IIS 已加载该 applicationinsights.dll。If you need to confirm that Application Insights is successfully attached, you can run Sysinternals Handle in a command window to confirm that applicationinsights.dll has been loaded by IIS.

    handle.exe /p w3wp.exe
    

无法连接?Can't connect? 没有遥测数据?No telemetry?

无法登录Unable to login

  • 如果不能登录状态监视器,请改为安装命令行。If Status Monitor cannot login, do a command line install instead. 状态监视器尝试登录以收集 ikey,但可使用以下命令手动提供该信息:Status Monitor attempts to login to collect your ikey, but you can provide this manually using the command:
Import-Module 'C:\Program Files\Microsoft Application Insights\Status Monitor\PowerShell\Microsoft.Diagnostics.Agent.StatusMonitor.PowerShell.dll'
Start-ApplicationInsightsMonitoring -Name appName -InstrumentationKey 00000000-000-000-000-0000000

无法加载文件或程序集“System.Diagnostics.DiagnosticSource”Could not load file or assembly 'System.Diagnostics.DiagnosticSource'

启用 Application Insights 后,可能会收到此错误。You may get this error after enabling Application Insights. 这是因为安装程序将在 bin 目录中替换此 dll。This is because the installer replaces this dll in your bin directory. 若要修复此错误,请更新 web.config:To fix update your web.config:

<dependentAssembly>
    <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51"/>
    <bindingRedirect oldVersion="0.0.0.0-4.*.*.*" newVersion="4.0.2.1"/>
</dependentAssembly>

我们将在此处跟踪此问题。We are tracking this issue here.

应用程序诊断消息Application diagnostic messages

  • 打开状态监视器,并在左窗格中选择应用程序。Open Status Monitor and select your application on left pane. 检查“配置通知”部分中是否有任何关于此应用程序的诊断消息:Check if there are any diagnostics messages for this application in the "Configuration notifications" section:

    打开“性能”边栏选项卡,查看请求、响应时间、依赖项和其他数据

详细日志Detailed logs

  • 默认情况下,状态监视器输出诊断日志的路径如下:C:\Program Files\Microsoft Application Insights\Status Monitor\diagnostics.logBy default Status Monitor will output diagnostic logs at: C:\Program Files\Microsoft Application Insights\Status Monitor\diagnostics.log

  • 若要输出详细日志,请修改配置文件:C:\Program Files\Microsoft Application Insights\Status Monitor\Microsoft.Diagnostics.Agent.StatusMonitor.exe.config 并将 <add key="TraceLevel" value="All" /> 添加到 appsettingsTo output verbose logs, modify the config file: C:\Program Files\Microsoft Application Insights\Status Monitor\Microsoft.Diagnostics.Agent.StatusMonitor.exe.config and add <add key="TraceLevel" value="All" /> to the appsettings. 然后重启状态监视器。Then restart status monitor.

  • 由于状态监视器是一个 .NET 应用程序,因此还可以通过向配置文件添加适当的诊断来启用 .NET 跟踪As Status Monitor is a .NET application you can also enable .net tracing by adding the appropriate diagnostics to the config file. 例如,在某些情况下,通过配置网络跟踪来查看网络级别发生的情况可能会很有用。For example, in some scenarios it can be useful to see what's happening at the network level by configuring network tracing

权限不足Insufficient permissions

  • 如果在服务器上看到有关“权限不足”的消息,请尝试以下操作:On the server, if you see a message about "insufficient permissions", try the following:
    • 在 IIS 管理器中选择应用程序池,打开“高级设置”,并记下“进程模型”下的标识 。In IIS Manager, select your application pool, open Advanced Settings, and under Process Model note the identity.
    • 在计算机管理控制面板中,将此标识添加到性能监试器用户组。In Computer management control panel, add this identity to the Performance Monitor Users group.

与 Systems Center Operations Manager 发生冲突Conflict with Systems Center Operations Manager

  • 如果在服务器上安装了 MMA/SCOM (Systems Center Operations Manager),某些版本可能会发生冲突。If you have MMA/SCOM (Systems Center Operations Manager) installed on your server, some versions can conflict. 请卸载 SCOM 和状态监视器,并重新安装最新版本。Uninstall both SCOM and Status Monitor, and re-install the latest versions.

安装失败或不完整Failed or incomplete installation

如果状态监视器在安装过程中失败,则可能出现不完整安装,状态监视器无法从中恢复。If Status Monitor fails during an installation, you could be left with an incomplete install that Status Monitor is unable to recover from. 这需要手动重置。This will require a manual reset.

删除应用程序目录中找到的以下任何文件:Delete any of these files found in your application directory:

  • bin 目录中以“Microsoft.AI.”Any DLLs in your bin directory starting with either "Microsoft.AI." 或“Microsoft.ApplicationInsights.”开头的所有 DLL。or "Microsoft.ApplicationInsights.".
  • bin 目录中的“Microsoft.Web.Infrastructure.dll”DLLThis DLL in your bin directory "Microsoft.Web.Infrastructure.dll"
  • bin 目录中的“System.Diagnostics.DiagnosticSource.dll”DLLThis DLL in your bin directory "System.Diagnostics.DiagnosticSource.dll"
  • 在应用程序目录中,删除“App_Data\packages”In your application directory remove "App_Data\packages"
  • 在应用程序目录中,删除“applicationinsights.config”In your application directory remove "applicationinsights.config"

更多故障排除方法Additional Troubleshooting

系统要求System Requirements

支持服务器上 Application Insights 状态监视器的 OS:OS support for Application Insights Status Monitor on Server:

  • Windows Server 2008Windows Server 2008
  • Windows Server 2008 R2Windows Server 2008 R2
  • Windows Server 2012Windows Server 2012
  • Windows server 2012 R2Windows server 2012 R2
  • Windows Server 2016Windows Server 2016

装有最新 SP 及 .NET Framework 4.5(状态监视器基于此版本的 Framework 构建)with latest SP and .NET Framework 4.5 (Status Monitor is built on this version of the framework)

在客户端:对于 Windows 7、8、8.1 和 10,同样需要安装 .NET Framework 4.5On the client side: Windows 7, 8, 8.1 and 10, again with .NET Framework 4.5

IIS 支持的是:IIS 7、7.5、8、8.5(IIS 是必需的)IIS support is: IIS 7, 7.5, 8, 8.5 (IIS is required)

使用 PowerShell 自动化Automation with PowerShell

可以在 IIS 服务器上使用 PowerShell 启动和停止监视。You can start and stop monitoring by using PowerShell on your IIS server.

首先导入 Application Insights 模块:First import the Application Insights module:

Import-Module 'C:\Program Files\Microsoft Application Insights\Status Monitor\PowerShell\Microsoft.Diagnostics.Agent.StatusMonitor.PowerShell.dll'

找出受监视的应用:Find out which apps are being monitored:

Get-ApplicationInsightsMonitoringStatus [-Name appName]

  • -Name (可选)Web 应用的名称。-Name (Optional) The name of a web app.

  • 显示此 IIS 服务器中每个 Web 应用(或命名应用)的 Application Insights 监视状态。Displays the Application Insights monitoring status for each web app (or the named app) in this IIS server.

  • 针对每个应用返回 ApplicationInsightsApplicationReturns ApplicationInsightsApplication for each app:

    • SdkState==EnabledAfterDeployment:应用正受到监视,已在运行时通过“状态监视器”工具或 Start-ApplicationInsightsMonitoring 进行检测。SdkState==EnabledAfterDeployment: App is being monitored, and was instrumented at run time, either by the Status Monitor tool, or by Start-ApplicationInsightsMonitoring.
    • SdkState==Disabled:未针对 Application Insights 检测应用。SdkState==Disabled: The app is not instrumented for Application Insights. 应用从未接受检测,或者“状态监视器”工具或 Stop-ApplicationInsightsMonitoring已禁用运行时监视。Either it was never instrumented, or run-time monitoring was disabled with the Status Monitor tool or with Stop-ApplicationInsightsMonitoring.
    • SdkState==EnabledByCodeInstrumentation:已通过将 SDK 添加到源代码来检测应用。SdkState==EnabledByCodeInstrumentation: The app was instrumented by adding the SDK to the source code. 其 SDK 无法更新或停止。Its SDK cannot be updated or stopped.
    • SdkVersion 显示正在用于监视此应用的版本。SdkVersion shows the version in use for monitoring this app.
    • LatestAvailableSdkVersion显示 NuGet 库中当前可用的版本。LatestAvailableSdkVersionshows the version currently available on the NuGet gallery. 要将应用升级到此版本,请使用 Update-ApplicationInsightsMonitoringTo upgrade the app to this version, use Update-ApplicationInsightsMonitoring.

Start-ApplicationInsightsMonitoring -Name appName -InstrumentationKey 00000000-000-000-000-0000000

  • -Name 应用在 IIS 中的名称-Name The name of the app in IIS

  • -InstrumentationKey 要在其中显示结果的 Application Insights 资源的 ikey。-InstrumentationKey The ikey of the Application Insights resource where you want the results to be displayed.

  • 此 cmdlet 只影响尚未检测的应用,即 SdkState==NotInstrumented。This cmdlet only affects apps that are not already instrumented - that is, SdkState==NotInstrumented.

    该 cmdlet 不会影响已检测的应用。The cmdlet does not affect an app that is already instrumented. 不管是构建时通过将 SDK 添加到代码来检测该应用,还是之前在运行时使用此 cmdlet 进行检测,都无关紧要。It does not matter whether the app was instrumented at build time by adding the SDK to the code, or at run time by a previous use of this cmdlet.

    用于检测应用的 SDK 版本是最近下载到此服务器的版本。The SDK version used to instrument the app is the version that was most recently downloaded to this server.

    若要下载最新版本,请使用 Update-ApplicationInsightsVersion。To download the latest version, use Update-ApplicationInsightsVersion.

  • 成功时返回 ApplicationInsightsApplicationReturns ApplicationInsightsApplication on success. 如果失败,则向 stderr 记录跟踪。If it fails, it logs a trace to stderr.

        Name                      : Default Web Site/WebApp1
        InstrumentationKey        : 00000000-0000-0000-0000-000000000000
        ProfilerState             : ApplicationInsights
        SdkState                  : EnabledAfterDeployment
        SdkVersion                : 1.2.1
        LatestAvailableSdkVersion : 1.2.3
    

Stop-ApplicationInsightsMonitoring [-Name appName | -All]

  • -Name 应用在 IIS 中的名称-Name The name of an app in IIS
  • -All 停止监视此 IIS 服务器中 SdkState==EnabledAfterDeployment 的所有应用-All Stops monitoring all apps in this IIS server for which SdkState==EnabledAfterDeployment
  • 停止监视指定的应用并删除检测。Stops monitoring the specified apps and removes instrumentation. 它仅适用于已在运行时使用“状态监视器”工具或 Start-ApplicationInsightsApplication 进行检测的应用。It only works for apps that have been instrumented at run-time using the Status Monitoring tool or Start-ApplicationInsightsApplication. (SdkState==EnabledAfterDeployment)(SdkState==EnabledAfterDeployment)
  • 返回 ApplicationInsightsApplication。Returns ApplicationInsightsApplication.

Update-ApplicationInsightsMonitoring -Name appName [-InstrumentationKey "0000000-0000-000-000-0000"]Update-ApplicationInsightsMonitoring -Name appName [-InstrumentationKey "0000000-0000-000-000-0000"]

  • -Name:Web 应用在 IIS 中的名称。-Name: The name of a web app in IIS.
  • -InstrumentationKey(可选。)使用此项可更改应用的遥测数据所要发送到的资源。-InstrumentationKey (Optional.) Use this to change the resource to which the app's telemetry is sent.
  • 此 cmdlet:This cmdlet:
    • 将命名应用升级到最近下载到此计算机的 SDK 版本。Upgrades the named app to the version of the SDK most recently downloaded to this machine. (仅当 SdkState==EnabledAfterDeployment时才适用)(Only works if SdkState==EnabledAfterDeployment)
    • 如果提供检测键,命名应用会重新配置为将遥测数据发送到具有该键的资源。If you provide an instrumentation key, the named app is reconfigured to send telemetry to the resource with that key. (仅当 SdkState != Disabled时才适用)(Works if SdkState != Disabled)

Update-ApplicationInsightsVersion

  • 将最新的 Application Insights SDK 下载到服务器。Downloads the latest Application Insights SDK to the server.

有关状态监视器的问题Questions about Status Monitor

什么是状态监视器?What is Status Monitor?

在 IIS Web 服务器中安装的桌面应用程序。A desktop application that you install in your IIS web server. 它有助于检测并配置 Web 应用。It helps you instrument and configure web apps.

何时使用状态监视器?When do I use Status Monitor?

  • 检测在 IIS 服务器上运行的任何 Web 应用,即使它已在运行。To instrument any web app that is running on your IIS server - even if it is already running.
  • 为 Web 应用启用其他遥测,此类应用已在编译时通过 Application Insights SDK 生成To enable additional telemetry for web apps that have been built with the Application Insights SDK at compile time.

可以在它运行后关闭它吗?Can I close it after it runs?

是的。Yes. 在它检测所选网站后,即可将其关闭。After it has instrumented the websites you select, you can close it.

它不会自行收集遥测。It doesn't collect telemetry by itself. 它只配置 Web 应用并设置某些权限。It just configures the web apps and sets some permissions.

状态监视器的作用是什么?What does Status Monitor do?

为要进行检测的状态监视器选择 Web 应用时,请执行以下操作:When you select a web app for Status Monitor to instrument:

  • 下载 Application Insights 程序集和 Application Insights.config 文件,并将其置于 Web 应用的二进制文件文件夹中。Downloads and places the Application Insights assemblies and ApplicationInsights.config file in the web app's binaries folder.
  • 启用 CLR 分析,以便收集依赖项调用。Enables CLR profiling to collect dependency calls.

状态监视器可安装哪个版本的 Application Insights SDK?What version of Application Insights SDK does Status Monitor install?

目前,状态监视器仅可安装 Application Insights SDK 版本 2.3 或 2.4。As of now, Status Monitor can only install Application Insights SDK versions 2.3 or 2.4.

Application Insights SDK 版本 2.4 是支持 .NET 4.0 的最新版本,已于 2016 年 1 月停产The Application Insights SDK Version 2.4 is the last version to support .NET 4.0 which was EOL January 2016. 因此,到目前为止,状态监视器可以用于检测 .NET 4.0 应用程序。Therefore, as of now Status Monitor can be used to instrument a .NET 4.0 application.

是否每次更新应用都需要运行状态监视器?Do I need to run Status Monitor whenever I update the app?

如果以增量方式重新进行部署,则不需要。Not if you redeploy incrementally.

如果在发布过程中选择了“删除现有文件”选项,则需重新运行状态监视器来配置 Application Insights。If you select the 'delete existing files' option in the publish process, you would need to re-run Status Monitor to configure Application Insights.

收集何种遥测?What telemetry is collected?

对于只在运行时使用状态监视器进行检测的应用程序:For applications that you instrument only at run-time by using Status Monitor:

  • HTTP 请求HTTP requests
  • 对依赖项的调用Calls to dependencies
  • 异常Exceptions
  • 性能计数器Performance counters

对于已在编译时进行检测的应用程序:For applications already instrumented at compile time:

  • 进程计数器。Process counters.
  • 依赖项调用 (.NET 4.5);返回依赖项调用中的值 (.NET 4.6)。Dependency calls (.NET 4.5); return values in dependency calls (.NET 4.6).
  • 异常堆栈跟踪值。Exception stack trace values.

了解详细信息Learn more

下载状态监视器Download Status Monitor

后续步骤Next steps

查看遥测:View your telemetry:

添加更多遥测:Add more telemetry: