Application Insight 也適用於地端伺服器 (on-premises servers),若您有內部伺服器使用 IIS 部署 ASP.NET 程式,則可以安裝 Application Insights Agent 進行監控。使用 Agent 最大的好處就是不需要修改程式碼並且重新部署,只需要透過 PowerShell 進行 線上下載檔案 方式安裝。本篇文章將簡單介紹如何部署 Application Insights Agent 於地端伺服器,若有錯誤或任何建議,請各為前輩不吝提出,謝謝。






在地端伺服器 IIS (ASP.NET) 使用 Application Insight Agent 影片有較詳細的說明,若喜歡歡迎訂閱我的 Youtube 頻道:Duran 技術冶煉廠





Step 1.  於 Azure 上建立 Application Insights 服務,並且記錄下檢測金鑰






Step 2. 透過 Agent 監控內部伺服器需要連線至 Application Insight 服務,需要開啟 outgoing 防火牆,才能將追蹤訊息上傳至雲端進行分析。詳細防火牆設定請參考:Azure 監視器所使用的 IP 位址




Step 3. 以系統管理者身份開啟 PowerShell,輸入下列指令安裝 NuGet  與 PowerShellGet


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




Step 4 安裝 Application Insights Agent (online) 


Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
Install-Module -Name Az.ApplicationMonitor -AllowPrerelease -AcceptLicense




Step 5  安裝 Application Insights Agent (offline)  與 Step 4 擇一期可
Step 5.1 前往 PowerShell Gallery: Az.ApplicationMonitor ,選擇手動下載






Step 5.2  將下載檔案複製到要安裝的主機,輸入下列指令進行安裝

$pathToNupkg = "存放路徑\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




Step 6.  輸入下列指令開始監控 (監控整個 IIS)

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
Enable-ApplicationInsightsMonitoring -InstrumentationKey '檢測金鑰' -AcceptLicense -EnableInstrumentationEngine





接下來在網站執行一些動作,到 Azure Application Insights 即可看見相關資料:




正常情況,應該是一個站台 對應一個 Azure Application Insights,你可以透過下列指令逐一設網站與 Application Insights 對應。


Enable-ApplicationInsightsMonitoring -InstrumentationKey @(@{MachineFilter='電腦名稱';AppFilter='網站名稱';InstrumentationSettings=@{InstrumentationKey='檢測金鑰'}}) -AcceptLicense -EnableInstrumentationEngine