你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
-
2025-10-13
Application Insights 代理是发布到 PowerShell 库的 PowerShell 模块。 它将替换状态监视器。 遥测数据将发送到 Azure 门户,你可以在其中监视应用。
有关支持的自动检测方案的完整列表,请参阅支持的环境、语言和资源提供程序。
注意
该模块当前支持通过 Internet 信息服务器(IIS )托管的 ASP.NET 和 ASP.NET Core Web 应用的无代码检测。 使用 SDK 检测 Java 和 Node.js 应用程序。
注意
默认情况下,ASP.NET Core 应用启用客户端监视。 如果要禁用客户端监视,请使用以下信息在服务器中定义环境变量:
-
名称:
APPINSIGHTS_JAVASCRIPT_ENABLED -
值:
false
PowerShell 库
Application Insights 代理位于 PowerShell 库中。
说明
- 若要开始使用简洁的代码示例,请参阅“入门”选项卡。
- 若要深入了解如何开始使用,请参阅“详细说明”选项卡。
- 有关 PowerShell API 参考,请参阅“API 参考”选项卡。
- 有关发行说明更新,请参阅“发行说明”选项卡。
此选项卡包含应适用于大多数环境的快速入门命令。 这些说明依赖于 PowerShell Gallery 来分发更新。 这些命令支持 PowerShell -Proxy 参数。
有关这些命令的说明、自定义说明以及有关故障排除的信息,请参阅详细说明。
如果没有 Azure 订阅,请在开始之前创建一个免费帐户。
通过 PowerShell 画廊下载并安装
使用 PowerShell Gallery 来下载和安装。
安装先决条件
若要启用监视,你必须有一个连接字符串。 连接字符串将显示在 Application Insights 资源的“概述”窗格上。 有关详细信息,请参阅连接字符串。
注意
有关可能需要的更多先决条件,请参阅 PowerShell 库传输层安全性(TLS)支持。
以管理员身份运行 PowerShell。
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
Install-Module -Name PowerShellGet -Force
关闭 PowerShell。
安装 Application Insights 代理
以管理员身份运行 PowerShell。
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
Install-Module -Name Az.ApplicationMonitor -AllowPrerelease -AcceptLicense
启用监视
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
Enable-ApplicationInsightsMonitoring -ConnectionString 'InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://xxxx.applicationinsights.azure.com/'
手动下载并安装(脱机选项)
还可以手动下载并安装。
下载模块
从 PowerShell 库手动下载最新版本的模块。
解压缩并安装 Application Insights 代理
$pathToNupkg = "C:\Users\t\Desktop\Az.ApplicationMonitor.0.3.0-alpha.nupkg"
$pathToZip = ([io.path]::ChangeExtension($pathToNupkg, "zip"))
$pathToNupkg | rename-item -newname $pathToZip
$pathInstalledModule = "$Env:ProgramFiles\WindowsPowerShell\Modules\Az.ApplicationMonitor"
Expand-Archive -LiteralPath $pathToZip -DestinationPath $pathInstalledModule
启用监视
Enable-ApplicationInsightsMonitoring -ConnectionString 'InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://xxxx.applicationinsights.azure.com/'
此选项卡介绍如何接入 PowerShell 画廊并下载 ApplicationMonitor 模块。 包含了开始使用所需的最常见参数。 我们还提供手动下载说明,以防你无法访问 Internet。
获取连接字符串
要开始,您需要一个连接字符串。 有关详细信息,请参阅连接字符串。
以管理员身份使用提升的执行策略运行 PowerShell
以管理员身份运行
PowerShell 需要拥有管理员级别的权限才能对计算机进行更改。
执行策略
- 说明:默认禁用 PowerShell 脚本的运行。 我们建议仅允许在当前作用域运行 "RemoteSigned" 脚本。
- 参考:关于执行策略和 Set-ExecutionPolicy。
- 命令:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process。 - 可选参数:
-
-Force。 跳过确认提示。
-
示例错误
Install-Module : The 'Install-Module' command was found in the module 'PowerShellGet', but the module could not be
loaded. For more information, run 'Import-Module PowerShellGet'.
Import-Module : File C:\Program Files\WindowsPowerShell\Modules\PackageManagement\1.3.1\PackageManagement.psm1 cannot
be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.
PowerShell 先决条件
运行 $PSVersionTable 命令,审核 PowerShell 的实例。
该命令生成以下输出:
Name Value
---- -----
PSVersion 5.1.17763.316
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.316
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
这些说明是在运行 Windows 10 和以下版本的计算机上编写和测试的。
PowerShell 库的先决条件
这些步骤准备服务器以从 PowerShell 库下载模块。
注意
PowerShell 图库支持 Windows 10、Windows Server 2016 和 PowerShell 6+。 有关早期版本的信息,请参阅安装 PowerShellGet。
以管理员身份使用提升的执行策略运行 PowerShell。
安装 NuGet 包提供程序。
- 说明:需要使用此提供程序才能与基于 NuGet 的存储库(例如 PowerShell Gallery)交互。
- 参考:Install-PackageProvider。
- 命令:
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201。 - 可选参数:
-
-Proxy。 指定请求的代理服务器。 -
-Force。 跳过确认提示。
-
如果未设置 NuGet,则会看到此提示:
NuGet provider is required to continue PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\t\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):将 PowerShell Gallery 配置为信任的存储库。
- 说明:默认情况下,PowerShell Gallery 是一个不受信任的存储库。
- 参考:Set-PSRepository。
- 命令:
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted。 - 可选参数:
-
-Proxy。 指定请求的代理服务器。
-
如果 PowerShell Gallery 不受信任,则会看到此提示:
Untrusted repository You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):可以通过运行
PSRepositories命令来确认此更改并审核所有Get-PSRepository。安装最新版本的 PowerShellGet。
- 说明:此模块包含用于从 PowerShell 库中获取其他模块的工具。 版本 1.0.0.1 与 Windows 10 和 Windows Server 一起发布。 需要 1.6.0 或更高版本。 若要确定安装了哪个版本,请运行
Get-Command -Module PowerShellGet命令。 - 参考:安装 PowerShellGet。
- 命令:
Install-Module -Name PowerShellGet。 - 可选参数:
-
-Proxy。 指定请求的代理服务器。 -
-Force。 忽略“已安装”警告并安装最新版本。
-
如果使用的不是最新 PowerShellGet 版本,则会看到此错误:
Install-Module : A parameter cannot be found that matches parameter name 'AllowPrerelease'. At line:1 char:20 Install-Module abc -AllowPrerelease ~~~~~~~~~~~~~~~~ CategoryInfo : InvalidArgument: (:) [Install-Module], ParameterBindingException FullyQualifiedErrorId : NamedParameterNotFound,Install-Module- 说明:此模块包含用于从 PowerShell 库中获取其他模块的工具。 版本 1.0.0.1 与 Windows 10 和 Windows Server 一起发布。 需要 1.6.0 或更高版本。 若要确定安装了哪个版本,请运行
重启 PowerShell。 无法在当前会话中加载新版本。 新的 PowerShell 会话加载新版的 PowerShellGet。
通过 PowerShell Gallery 下载并安装模块
这些步骤将从 PowerShell 库下载 Az.ApplicationMonitor 模块。
- 确保满足 PowerShell 库的所有先决条件。
- 以管理员身份使用提升的执行策略运行 PowerShell。
- 安装 Az.ApplicationMonitor 模块。
- 参考:Install-Module。
- 命令:
Install-Module -Name Az.ApplicationMonitor。 - 可选参数:
-
-Proxy。 指定请求的代理服务器。 -
-AllowPrerelease。 允许安装 alpha 和 beta 版本。 -
-AcceptLicense。 跳过“接受许可证”提示 -
-Force。 忽略“不受信任的存储库”警告。
-
手动下载并安装模块(脱机选项)
如果出于任何原因无法连接到 PowerShell 模块,可以手动下载并安装 Az.ApplicationMonitor 模块。
手动下载最新的 nupkg 文件
- 转到 https://www.powershellgallery.com/packages/Az.ApplicationMonitor。
- 在“版本历史记录”表中选择文件的最新版本。
- 在“安装选项”下选择“手动下载”。
选项 1:安装到 PowerShell 模块目录中
将手动下载的 PowerShell 模块安装到 PowerShell 目录中,使之可被 PowerShell 会话发现。 有关详细信息,请参阅安装 PowerShell 模块。
使用 Expand-Archive (v1.0.1.0) 将 nupkg 作为 zip 文件解压缩
说明:基础版本的 Microsoft.PowerShell.Archive (v1.0.1.0) 无法解压缩 nupkg 文件。 使用 .zip 扩展名重命名文件。
参考:Expand-Archive。
命令:
$pathToNupkg = "C:\az.applicationmonitor.0.3.0-alpha.nupkg" $pathToZip = ([io.path]::ChangeExtension($pathToNupkg, "zip")) $pathToNupkg | rename-item -newname $pathToZip $pathInstalledModule = "$Env:ProgramFiles\WindowsPowerShell\Modules\az.applicationmonitor" Expand-Archive -LiteralPath $pathToZip -DestinationPath $pathInstalledModule
使用 Expand-Archive (v1.1.0.0) 解压缩 nupkg
说明:使用最新版本的 Expand-Archive 解压缩 nupkg 文件但不更改扩展名。
命令:
$pathToNupkg = "C:\az.applicationmonitor.0.2.1-alpha.nupkg" $pathInstalledModule = "$Env:ProgramFiles\WindowsPowerShell\Modules\az.applicationmonitor" Expand-Archive -LiteralPath $pathToNupkg -DestinationPath $pathInstalledModule
选项 2:手动解压缩并导入 nupkg
将手动下载的 PowerShell 模块安装到 PowerShell 目录中,使之可被 PowerShell 会话发现。 有关详细信息,请参阅安装 PowerShell 模块。
若要将模块安装到任何其他目录中,请使用 Import-Module 手动导入模块。
重要
通过相对路径安装动态链接库(DLL)。 请将此包的内容存储到所需的运行时目录中,并确认访问权限允许读取但不允许写入。
- 将扩展名更改为“.zip”,并将包的内容解压缩到所需的安装目录中。
- 找到 Az.ApplicationMonitor.psd1 的文件路径。
- 以管理员身份使用提升的执行策略运行 PowerShell。
- 通过
Import-Module Az.ApplicationMonitor.psd1命令加载模块。
通过代理路由流量
监视专用 Intranet 中的计算机时,需要通过代理路由 HTTP 流量。
用于从 PowerShell 库下载并安装 Az.ApplicationMonitor 的 PowerShell 命令支持 -Proxy 参数。
编写安装脚本时,请查看上述说明。
Application Insights SDK 需要将应用的遥测数据发送给 Microsoft。 建议在 web.config 文件中配置应用的代理设置。 有关详细信息,请参阅如何实现代理直通?。
启用监视
使用 Enable-ApplicationInsightsMonitoring 命令以启用监视。
有关如何使用此 cmdlet 的详细说明,请参阅 API 参考。
此选项卡介绍以下 cmdlet,它们是 Az.ApplicationMonitor PowerShell 模块的成员:
- Enable-InstrumentationEngine
- Enable-ApplicationInsightsMonitoring
- Disable-InstrumentationEngine
- Disable-ApplicationInsightsMonitoring (禁用应用洞察监控)
- Get-ApplicationInsightsMonitoringConfig
- Get-ApplicationInsightsMonitoringStatus(获取应用程序洞察监控状态)
- Set-ApplicationInsightsMonitoringConfig
- Start-ApplicationInsightsMonitoringTrace
注意
- 要开始,您需要一个连接字符串。 有关详细信息,请参阅创建资源。
- 此 cmdlet 要求查看并接受我们的许可条款和隐私声明。
重要
此 cmdlet 要求使用管理员权限和提升的执行策略建立 PowerShell 会话。 有关详细信息,请参阅以管理员身份运行 PowerShell 并使用提升的执行策略。
- 此 cmdlet 要求查看并接受我们的许可条款和隐私声明。
- 检测引擎会增加更多开销,默认情况下处于关闭状态。
Enable-InstrumentationEngine
通过设置一些注册表项启用检测引擎。 重启 IIS 以使这些更改生效。
检测引擎可以补充 .NET 软件开发工具包(SDK)收集的数据。 它收集描述托管进程执行的事件和消息。 这些事件和消息包括依赖项结果代码、HTTP 谓词和 SQL 命令文本。
在以下情况下启用检测引擎:
- 已使用
Enablecmdlet 启用了监视,但未启用检测引擎。 - 您已使用 .NET SDK 手动配置应用,并且希望收集额外的遥测数据。
示例
Enable-InstrumentationEngine
参数
-接受许可
可选。 使用此开关可在无界面安装中接受许可条款和隐私声明。
-Verbose
通用参数。 使用此开关可输出详细日志。
输出
成功启用检测引擎的示例输出
Configuring IIS Environment for instrumentation engine...
Configuring registry for instrumentation engine...
Enable-ApplicationInsightsMonitoring
对目标计算机上的 IIS 应用启用无代码附加监视。
此 cmdlet 会修改 IIS applicationHost.config 并设置一些注册表项。 它会创建 applicationinsights.ikey.config 文件,该文件定义每个应用使用的检测密钥。 IIS 在启动时会加载 RedfieldModule,从而在应用程序启动时将 Application Insights SDK 注入到应用程序中。 重启 IIS 以使更改生效。
启用监视后,我们建议你使用实时指标快速检查应用是否正在向我们发送遥测数据。
示例
单个连接字符串的示例
在此示例中,当前计算机上的所有应用都获得了同一个连接字符串。
Enable-ApplicationInsightsMonitoring -ConnectionString 'InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;IngestionEndpoint=https://xxxx.applicationinsights.azure.com/'
使用单个仪器密钥的示例
在此示例中,当前计算机上的所有应用都获得了同一个检测密钥。
Enable-ApplicationInsightsMonitoring -InstrumentationKey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
使用检测密钥映射的示例
在本示例中:
-
MachineFilter使用'.*'通配符匹配当前计算机。 -
AppFilter='WebAppExclude'提供null工具密钥。 未检测指定的应用。 -
AppFilter='WebAppOne'为指定的应用分配唯一的检测密钥。 -
AppFilter='WebAppTwo'为指定的应用分配唯一的检测密钥。 -
AppFilter使用'.*'通配符来匹配它尚未匹配的任何 Web 应用,并分配默认检测密钥。 - 添加空格以提高可读性。
Enable-ApplicationInsightsMonitoring -InstrumentationKeyMap `
` @(@{MachineFilter='.*';AppFilter='WebAppExclude'},
` @{MachineFilter='.*';AppFilter='WebAppOne';InstrumentationSettings=@{InstrumentationKey='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx1'}},
` @{MachineFilter='.*';AppFilter='WebAppTwo';InstrumentationSettings=@{InstrumentationKey='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx2'}},
` @{MachineFilter='.*';AppFilter='.*';InstrumentationSettings=@{InstrumentationKey='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxdefault'}})
-
MachineFilter 是计算机或虚拟机(VM)名称必需的 C# 正则表达式。
- “.*”匹配所有项
- “ComputerName”仅匹配具有指定确切名称的计算机。
-
AppFilter 是 IIS 站点名称所需的 C# 正则表达式。 可以通过运行命令 get-iissite 获取服务器上的站点列表。
- “.*”匹配所有项
- “SiteName”仅匹配具有指定确切名称的 IIS 站点。
- 需要 InstrumentationKey 才能监视与上述两个筛选器匹配的应用。
- 如果要定义排除监视的规则,请将此值保留为 null。
-EnableInstrumentationEngine
可选。 使用此开关可让检测引擎收集执行托管进程期间发生的事件和相关消息。 这些事件和消息包括依赖项结果代码、HTTP 谓词和 SQL 命令文本。
检测引擎会增加开销,默认情况下处于关闭状态。
-接受许可
可选。 使用此开关可在无界面安装中接受许可条款和隐私声明。
-IgnoreSharedConfig
当你使用一组 Web 服务器时,你可能正在使用共享配置。 HttpModule 无法注入此共享配置。 此脚本将失败,并提示需要额外的安装步骤。 使用此开关可忽略该检查并继续安装必备组件。 有关更多信息,请参阅与IIS共享配置已知的冲突
-Verbose
通用参数。 使用此开关可显示详细日志。
-WhatIf
通用参数。 使用此开关可以测试和验证输入参数,而无需真正启用监视。
输出
成功启用后的示例输出
Initiating Disable Process
Applying transformation to 'C:\Windows\System32\inetsrv\config\applicationHost.config'
'C:\Windows\System32\inetsrv\config\applicationHost.config' backed up to 'C:\Windows\System32\inetsrv\config\applicationHost.config.backup-2019-03-26_08-59-52z'
in :1,237
No element in the source document matches '/configuration/location[@path='']/system.webServer/modules/add[@name='ManagedHttpModuleHelper']'
Not executing RemoveAll (transform line 1, 546)
Transformation to 'C:\Windows\System32\inetsrv\config\applicationHost.config' was successfully applied. Operation: 'disable'
GAC Module will not be removed, since this operation might cause IIS instabilities
Configuring IIS Environment for codeless attach...
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\IISADMIN[Environment]
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC[Environment]
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WAS[Environment]
Configuring IIS Environment for instrumentation engine...
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\IISADMIN[Environment]
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC[Environment]
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WAS[Environment]
Configuring registry for instrumentation engine...
Successfully disabled Application Insights Agent
Installing GAC module 'C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\0.2.0\content\Runtime\Microsoft.AppInsights.IIS.ManagedHttpModuleHelper.dll'
Applying transformation to 'C:\Windows\System32\inetsrv\config\applicationHost.config'
Found GAC module Microsoft.AppInsights.IIS.ManagedHttpModuleHelper.ManagedHttpModuleHelper, Microsoft.AppInsights.IIS.ManagedHttpModuleHelper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
'C:\Windows\System32\inetsrv\config\applicationHost.config' backed up to 'C:\Windows\System32\inetsrv\config\applicationHost.config.backup-2019-03-26_08-59-52z_1'
Transformation to 'C:\Windows\System32\inetsrv\config\applicationHost.config' was successfully applied. Operation: 'enable'
Configuring IIS Environment for codeless attach...
Configuring IIS Environment for instrumentation engine...
Configuring registry for instrumentation engine...
Updating app pool permissions...
Successfully enabled Application Insights Agent
Disable-InstrumentationEngine
通过删除一些注册表项来禁用检测引擎。 重启 IIS 以使这些更改生效。
示例
Disable-InstrumentationEngine
参数
-Verbose
通用参数。 使用此开关可输出详细日志。
输出
成功禁用检测引擎的示例输出
Configuring IIS Environment for instrumentation engine...
Registry: removing 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\IISADMIN[Environment]'
Registry: removing 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC[Environment]'
Registry: removing 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WAS[Environment]'
Configuring registry for instrumentation engine...
Disable-ApplicationInsightsMonitoring
在目标计算机上禁用监视。 此 cmdlet 会删除对 IIS applicationHost.config 的编辑并删除注册表项。
示例
Disable-ApplicationInsightsMonitoring
参数
-Verbose
通用参数。 使用此开关可显示详细日志。
输出
成功禁用监视的示例输出
Initiating Disable Process
Applying transformation to 'C:\Windows\System32\inetsrv\config\applicationHost.config'
'C:\Windows\System32\inetsrv\config\applicationHost.config' backed up to 'C:\Windows\System32\inetsrv\config\applicationHost.config.backup-2019-03-26_08-59-00z'
in :1,237
No element in the source document matches '/configuration/location[@path='']/system.webServer/modules/add[@name='ManagedHttpModuleHelper']'
Not executing RemoveAll (transform line 1, 546)
Transformation to 'C:\Windows\System32\inetsrv\config\applicationHost.config' was successfully applied. Operation: 'disable'
GAC Module will not be removed, since this operation might cause IIS instabilities
Configuring IIS Environment for codeless attach...
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\IISADMIN[Environment]
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC[Environment]
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WAS[Environment]
Configuring IIS Environment for instrumentation engine...
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\IISADMIN[Environment]
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC[Environment]
Registry: skipping non-existent 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WAS[Environment]
Configuring registry for instrumentation engine...
Successfully disabled Application Insights Agent
Get-ApplicationInsightsMonitoringConfig
获取配置文件并将值输出到控制台。
示例
Get-ApplicationInsightsMonitoringConfig
参数
不需要参数。
输出
读取配置文件的示例输出
RedfieldConfiguration:
Filters:
0)InstrumentationKey: AppFilter: WebAppExclude MachineFilter: .*
1)InstrumentationKey: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx2 AppFilter: WebAppTwo MachineFilter: .*
2)InstrumentationKey: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxdefault AppFilter: .* MachineFilter: .*
Get-ApplicationInsightsMonitoringStatus
此 cmdlet 提供有关 Application Insights 代理的故障排除信息。 使用此 cmdlet 调查监视状态、PowerShell 模块的版本,以及检查正在运行的进程。 此 cmdlet 会报告监视所需的版本信息和密钥文件相关信息。
示例
示例:应用程序状态
运行 Get-ApplicationInsightsMonitoringStatus 命令来显示网站的监视状态。
Get-ApplicationInsightsMonitoringStatus
IIS Websites:
SiteName : Default Web Site
ApplicationPoolName : DefaultAppPool
SiteId : 1
SiteState : Stopped
SiteName : DemoWebApp111
ApplicationPoolName : DemoWebApp111
SiteId : 2
SiteState : Started
ProcessId : not found
SiteName : DemoWebApp222
ApplicationPoolName : DemoWebApp222
SiteId : 3
SiteState : Started
ProcessId : 2024
Instrumented : true
InstrumentationKey : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx123
SiteName : DemoWebApp333
ApplicationPoolName : DemoWebApp333
SiteId : 4
SiteState : Started
ProcessId : 5184
AppAlreadyInstrumented : true
在本示例中:
计算机标识符是一个匿名 ID,用于唯一标识服务器。 如果你创建支持请求,我们需要该 ID 来查找你的服务器的日志。
“默认网站”在 IIS 中处于“已停止”状态
IIS 会显示 DemoWebApp111 已启动,但应用不会收到任何请求。 此报告显示没有正在运行的进程(ProcessId: 未找到)。
DemoWebApp222 正在运行,并且正在被监控(已仪器化:是)。 根据用户配置,此站点的检测密钥 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx123 匹配。
DemoWebApp333 使用 Application Insights SDK 手动进行检测。 Application Insights 代理检测该 SDK 而不监视此站点。
存在
AppAlreadyInstrumented : true表示 Application Insights 代理标识了在 Web 应用程序中加载的冲突 dll,假定 Web 应用已手动检测,并且代理已退让且未检测此过程。Instrumented : true指示 Application Insights 代理已成功检测在指定的 w3wp.exe 进程中运行的 Web 应用。
示例:PowerShell 模块信息
运行 Get-ApplicationInsightsMonitoringStatus -PowerShellModule 命令可显示有关当前模块的信息:
Get-ApplicationInsightsMonitoringStatus -PowerShellModule
PowerShell Module version:
0.4.0-alpha
Application Insights SDK version:
2.9.0.3872
Executing PowerShell Module Assembly:
Microsoft.ApplicationInsights.Redfield.Configurator.PowerShell, Version=2.8.14.11432, Culture=neutral, PublicKeyToken=31bf3856ad364e35
PowerShell Module Directory:
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\0.2.2\content\PowerShell
Runtime Paths:
ParentDirectory (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content
ConfigurationPath (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\applicationInsights.ikey.config
ManagedHttpModuleHelperPath (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.AppInsights.IIS.ManagedHttpModuleHelper.dll
RedfieldIISModulePath (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.ApplicationInsights.RedfieldIISModule.dll
InstrumentationEngine86Path (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Instrumentation32\MicrosoftInstrumentationEngine_x86.dll
InstrumentationEngine64Path (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Instrumentation64\MicrosoftInstrumentationEngine_x64.dll
InstrumentationEngineExtensionHost86Path (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Instrumentation32\Microsoft.ApplicationInsights.ExtensionsHost_x86.dll
InstrumentationEngineExtensionHost64Path (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Instrumentation64\Microsoft.ApplicationInsights.ExtensionsHost_x64.dll
InstrumentationEngineExtensionConfig86Path (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Instrumentation32\Microsoft.InstrumentationEngine.Extensions.config
InstrumentationEngineExtensionConfig64Path (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Instrumentation64\Microsoft.InstrumentationEngine.Extensions.config
ApplicationInsightsSdkPath (Exists: True)
C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.ApplicationInsights.dll
示例:运行时状态
可以检查已检测计算机上的进程以查看是否已加载所有 DLL。 如果监视功能在正常工作,则至少有 12 个 DLL 应加载。
运行命令 Get-ApplicationInsightsMonitoringStatus -InspectProcess:
Get-ApplicationInsightsMonitoringStatus -InspectProcess
iisreset.exe /status
Status for IIS Admin Service ( IISADMIN ) : Running
Status for Windows Process Activation Service ( WAS ) : Running
Status for Net.Msmq Listener Adapter ( NetMsmqActivator ) : Running
Status for Net.Pipe Listener Adapter ( NetPipeActivator ) : Running
Status for Net.Tcp Listener Adapter ( NetTcpActivator ) : Running
Status for World Wide Web Publishing Service ( W3SVC ) : Running
handle64.exe -accepteula -p w3wp
BF0: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.AI.ServerTelemetryChannel.dll
C58: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.AI.AzureAppServices.dll
C68: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.AI.DependencyCollector.dll
C78: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.AI.WindowsServer.dll
C98: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.AI.Web.dll
CBC: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.AI.PerfCounterCollector.dll
DB0: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.AI.Agent.Intercept.dll
B98: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.ApplicationInsights.RedfieldIISModule.dll
BB4: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.ApplicationInsights.RedfieldIISModule.Contracts.dll
BCC: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.ApplicationInsights.Redfield.Lightup.dll
BE0: File (R-D) C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.ApplicationInsights.dll
listdlls64.exe -accepteula w3wp
0x0000000019ac0000 0x127000 C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Instrumentation64\MicrosoftInstrumentationEngine_x64.dll
0x00000000198b0000 0x4f000 C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Instrumentation64\Microsoft.ApplicationInsights.ExtensionsHost_x64.dll
0x000000000c460000 0xb2000 C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Instrumentation64\Microsoft.ApplicationInsights.Extensions.Base_x64.dll
0x000000000ad60000 0x108000 C:\Windows\TEMP\2.4.0.0.Microsoft.ApplicationInsights.Extensions.Intercept_x64.dll
参数
(不带参数)
默认情况下,此 cmdlet 会报告 Web 应用程序的监视状态。 使用此选项来查看是否已成功检测应用程序。 您还可以查看哪个工具密钥已与您的网站匹配。
-PowerShellModule
可选。 使用此开关报告监视所需的 DLL 的版本号和路径。 如果需要标识任何 DLL 的版本(包括 Application Insights SDK),请使用此选项。
-InspectProcess
可选。 使用此开关报告 IIS 是否正在运行。 它会下载外部工具,以确定是否将必要的 DLL 加载到 IIS 运行时。
如果此过程因任何原因失败,你可以手动运行这些命令:
iisreset.exe /status[handle64.exe](/sysinternals/downloads/handle) -p w3wp | findstr /I "InstrumentationEngine AI. ApplicationInsights"[listdlls64.exe](/sysinternals/downloads/listdlls) w3wp | findstr /I "InstrumentationEngine AI ApplicationInsights"
-强制
可选。 仅与 InspectProcess 一起使用。 使用此开关可跳过在下载更多工具之前出现的用户提示。
Set-ApplicationInsightsMonitoringConfig
在不进行完全重新安装的情况下设置配置文件。 重启 IIS 以使更改生效。
重要
此 cmdlet 需要具有管理员权限的 PowerShell 会话。
示例
使用单个仪器密钥的示例
在此示例中,当前计算机上的所有应用都获得了同一个检测密钥。
Enable-ApplicationInsightsMonitoring -InstrumentationKey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
使用检测密钥映射的示例
在本示例中:
-
MachineFilter使用'.*'通配符匹配当前计算机。 -
AppFilter='WebAppExclude'提供null工具密钥。 未检测指定的应用。 -
AppFilter='WebAppOne'为指定的应用分配唯一的检测密钥。 -
AppFilter='WebAppTwo'为指定的应用分配唯一的检测密钥。 -
AppFilter使用'.*'通配符来匹配它尚未匹配的 Web 应用,并分配默认检测密钥。 - 添加空格以提高可读性。
Enable-ApplicationInsightsMonitoring -InstrumentationKeyMap `
` @(@{MachineFilter='.*';AppFilter='WebAppExclude'},
` @{MachineFilter='.*';AppFilter='WebAppOne';InstrumentationSettings=@{InstrumentationKey='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx1'}},
` @{MachineFilter='.*';AppFilter='WebAppTwo';InstrumentationSettings=@{InstrumentationKey='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx2'}},
` @{MachineFilter='.*';AppFilter='.*';InstrumentationSettings=@{InstrumentationKey='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxdefault'}})
参数
-InstrumentationKey
必需。 使用此参数提供单个检测密钥以供目标计算机上的所有应用使用。
-InstrumentationKeyMap
必需。 使用此参数可提供多个检测密钥以及每个应用使用的检测密钥映射。
通过设置 MachineFilter,可以为多台计算机创建单个安装脚本。
重要
应用按提供规则的顺序匹配规则。 首先指定最特定的规则,最后指定最通用的规则。
重要
InstrumentationKeyMap 是一项高级路由功能,可将同一计算机上的 Internet 信息服务(IIS)应用映射到 Application Insights 资源。 此功能适用于 Application Insights 代理自动检测的 IIS 托管的 ASP.NET 和 ASP.NET Core 应用。
匹配过程的工作原理
- 映射定义了一个名为
filters的规则已排序列表。 第一个匹配规则生效。 先放置具体特定的规则,最后放置一条通用规则。 - 每个规则可以为匹配的应用分配不同的 Application Insights 资源。 在受支持的方案中优先使用连接字符串,因为检测密钥已属于旧版内容。
可用筛选器
-
MachineFilter或machineFilter: 与计算机或虚拟机(VM)名称匹配的 C# 正则表达式。.*匹配所有名称。 -
AppFilter或appFilter: 与 IIS 站点名称(HostingEnvironment.SiteName)匹配的 C# 正则表达式。 如果未提供VirtualPathFilter或virtualPathFilter,则需要此筛选器。 -
VirtualPathFilter或virtualPathFilter: 与 IIS 虚拟路径(HostingEnvironment.ApplicationVirtualPath)匹配的 C# 正则表达式。 使用此筛选器可将网站下的单个应用作为目标。
术语映射
- PowerShell cmdlet 使用
MachineFilter、AppFilter和VirtualPathFilter。 - Azure VM 和虚拟机规模集扩展 JSON 使用
machineFilter、appFilter和virtualPathFilter,并使用instrumentationSettings设置资源。
小窍门
包含一个适用于所有所有应用(如 .*)的最终规则,并应用默认资源以明确其行为。
此 cmdlet 的映射形状
- 提供
-InstrumentationKeyMap作为 PowerShell 哈希表数组。 - 对于此 cmdlet,使用
InstrumentationSettings=@{ InstrumentationKey = '<ikey>' }按规则设置目标资源。 - 如果想为计算机上的所有应用使用同一个资源,请改用
-ConnectionString或-InstrumentationKey。
Start-ApplicationInsightsMonitoringTrace
收集无代码附加运行时发出的 Windows 事件跟踪(ETW)事件。 使用此 cmdlet 作为运行 PerfView 的更简单的替代方法。
会实时将事件打印到控制台,同时写入 .etl 文件。 可以使用 PerfView 打开 .etl 文件以进行更深入的分析。
此 cmdlet 一直运行至超时,默认值为 5 分钟,或运行到手动使用 Ctrl + C 将其停止。
示例
如何收集事件
在需要调查未检测 IIS 应用的原因时使用此流。
无代码附加运行时在 IIS 启动及应用启动时发出 ETW 事件。
- 在管理命令提示符下,运行
iisreset /stop以停止 IIS 和所有 Web 应用。 - 通过运行此 cmdlet 开始跟踪。
- 在管理命令提示符下,运行
iisreset /start以启动 IIS。 - 通过浏览到应用触发启动。
- 在应用完成加载后,按下
Ctrl + C停止,或允许超时以结束会话。
要收集的事件
可以选择要包括的事件源:
-
-CollectSdkEvents从 Application Insights SDK 收集事件。 -
-CollectRedfieldEvents从 Application Insights 代理和 Redfield 运行时收集事件,这有助于 IIS 和应用的启动诊断。 - 通过指定两个开关来收集这两个集。
- 如果未指定开关,则默认收集这两个集。
参数
-最大持续时间(分钟)
可选。 设置超时前收集的时间。默认值为 5 分钟。
-LogDirectory
可选。 用于写入 .etl 文件的目录。 默认情况下,文件在模块 PowerShell 目录下创建。 会话开始时会显示完整路径。
-CollectSdkEvents
可选。 包括 Application Insights SDK 事件。
-CollectRedfieldEvents
可选。 包括来自 Application Insights 代理和 Redfield 运行时的事件。
-Verbose
通用参数。 输出的详细日志。
输出
应用程序启动日志的示例
Start-ApplicationInsightsMonitoringTrace -CollectRedfieldEvents
Starting...
Log File: C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\logs\20190627_144217_ApplicationInsights_ETW_Trace.etl
Tracing enabled, waiting for events.
Tracing will timeout in 5 minutes. Press CTRL+C to cancel.
2:42:31 PM EVENT: Microsoft-ApplicationInsights-IIS-ManagedHttpModuleHelper Trace Resolved variables to: MicrosoftAppInsights_ManagedHttpModulePath='C:\Program Files\WindowsPowerShell\Modules\Az.ApplicationMonitor\content\Runtime\Microsoft.ApplicationInsights.RedfieldIISModule.dll', MicrosoftAppInsights_ManagedHttpModuleType='Microsoft.ApplicationInsights.RedfieldIISModule.RedfieldIISModule'
2:42:31 PM EVENT: Microsoft-ApplicationInsights-IIS-ManagedHttpModuleHelper Trace Resolved variables to: MicrosoftDiagnosticServices_ManagedHttpModulePath2='', MicrosoftDiagnosticServices_ManagedHttpModuleType2=''
2:42:31 PM EVENT: Microsoft-ApplicationInsights-IIS-ManagedHttpModuleHelper Trace Environment variable 'MicrosoftDiagnosticServices_ManagedHttpModulePath2' or 'MicrosoftDiagnosticServices_ManagedHttpModuleType2' is null, skipping managed dll loading
反馈
此页面是否有帮助?
否
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
此页面是否有帮助?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?