適用於 Azure VM 和虛擬機擴展集的 Application Insights

為在 Azure 虛擬機器Azure 虛擬機器擴展集 上執行的 ASP.NET 和 ASP.NET Core IIS 裝載應用程式啟用監視,現在比以往更容易。 在不修改程式代碼的情況下,取得使用 Application Insights 的所有優點。

本文將逐步引導您使用 Application Insights 代理程式啟用 Application Insights 監視。 它也提供將大規模部署程式自動化的初步指引。

啟用 Application Insights

自動結構很容易啟用。 不需要進階設定。

如需支援自動結構案例的完整清單,請參閱 支援的環境、語言和資源提供者

注意

自動結構適用於 ASP.NET、ASP.NET Core IIS 裝載的應用程式和 Java。 使用 SDK 來檢測裝載在 Azure 虛擬機和虛擬機擴展集上的Node.js和 Python 應用程式。

Application Insights 代理程式會自動收集與 SDK 相同的相依性訊號。 若要深入瞭解,請參閱 相依性自動收集

安裝 Application Insights 代理程式擴充功能之前,您需要 連接字串。 建立新的 Application Insights 資源,或從現有的 Application Insights 資源複製 連接字串。

啟用虛擬機的監視

您可以使用 Azure 入口網站 或 PowerShell 來啟用 VM 的監視。

Azure 入口網站

  1. 在 Azure 入口網站 中,移至 Application Insights 資源。 將 連接字串 複製到剪貼簿。

    顯示 連接字串 的螢幕快照。

  2. 移至您的虛擬機。 在左側功能表中的 [設定] 區段下,選取 [擴充功能 + 應用程式>新增]。

    顯示 [擴充功能 + 應用程式] 窗格的螢幕快照,其中包含 [新增] 按鈕。

  3. 選取 [Application Insights 代理程式>] [下一步]。

    顯示 [安裝延伸模組] 窗格的螢幕快照,其中包含 [下一步] 按鈕。

  4. 貼上您在步驟 1 中複製的 連接字串,然後選取 [檢閱 + 建立]。

    顯示 [建立] 索引標籤的螢幕快照,其中包含 [檢閱 + 建立] 按鈕。

PowerShell

注意

您不熟悉 PowerShell 嗎? 請參閱 開始使用指南

安裝或更新 Application Insights 代理程式作為 Azure 虛擬機的擴充功能:

# define variables to match your environment before running
$ResourceGroup = "<myVmResourceGroup>"
$VMName = "<myVmName>"
$Location = "<myVmLocation>"
$ConnectionString = "<myAppInsightsResourceConnectionString>"

$publicCfgJsonString = @"
{
    "redfieldConfiguration": {
        "instrumentationKeyMap": {
        "filters": [
            {
            "appFilter": ".*",
            "machineFilter": ".*",
            "virtualPathFilter": ".*",
            "instrumentationSettings" : {
                "connectionString": "$ConnectionString"
            }
            }
        ]
        }
    }
    }
"@

$privateCfgJsonString = '{}'
	
Set-AzVMExtension -ResourceGroupName $ResourceGroup -VMName $VMName -Location $Location -Name "ApplicationMonitoringWindows" -Publisher "Microsoft.Azure.Diagnostics" -Type "ApplicationMonitoringWindows" -Version "2.8" -SettingString $publicCfgJsonString -ProtectedSettingString $privateCfgJsonString

注意

如需更複雜的大規模部署,您可以使用PowerShell迴圈,跨多個 VM 安裝或更新 Application Insights 代理程式擴充功能。

查詢 Azure 虛擬機的 Application Insights 代理程式擴充功能狀態:

Get-AzVMExtension -ResourceGroupName "<myVmResourceGroup>" -VMName "<myVmName>" -Name ApplicationMonitoringWindows -Status

取得適用於 Azure 虛擬機器的已安裝擴充功能清單:

Get-AzResource -ResourceId "/subscriptions/<mySubscriptionId>/resourceGroups/<myVmResourceGroup>/providers/Microsoft.Compute/virtualMachines/<myVmName>/extensions"

從 Azure 虛擬機卸載 Application Insights 代理程式擴充功能:

Remove-AzVMExtension -ResourceGroupName "<myVmResourceGroup>" -VMName "<myVmName>" -Name "ApplicationMonitoring"

注意

選取與用來部署Application Insights代理程式擴充功能之 連接字串 相關聯的Application Insights 資源內的即時計量串流,以確認安裝。 如果您要從多部虛擬機傳送數據,請選取 [伺服器名稱] 底下的目標 Azure 虛擬機。 可能需要一分鐘的時間,數據才會開始流動。

啟用虛擬機擴展集的監視

您可以使用 Azure 入口網站 或 PowerShell 來啟用虛擬機擴展集的監視。

Azure 入口網站

請遵循 VM 的先前步驟,但請移至虛擬機擴展集,而不是您的 VM。

PowerShell

安裝或更新 Application Insights 代理程式作為虛擬機擴展集的擴充功能:

# Set resource group, vmss name, and connection string to reflect your environment
$ResourceGroup = "<myVmResourceGroup>"
$VMSSName = "<myVmName>"
$ConnectionString = "<myAppInsightsResourceConnectionString>"
$publicCfgHashtable =
@{
  "redfieldConfiguration"= @{
    "instrumentationKeyMap"= @{
      "filters"= @(
        @{
          "appFilter"= ".*";
          "machineFilter"= ".*";
          "virtualPathFilter"= ".*";
          "instrumentationSettings" = @{
            "connectionString"= "$ConnectionString"
          }
        }
      )
    }
  }
};
$privateCfgHashtable = @{};
$vmss = Get-AzVmss -ResourceGroupName $ResourceGroup -VMScaleSetName $VMSSName
Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "ApplicationMonitoringWindows" -Publisher "Microsoft.Azure.Diagnostics" -Type "ApplicationMonitoringWindows" -TypeHandlerVersion "2.8" -Setting $publicCfgHashtable -ProtectedSetting $privateCfgHashtable
Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -Name $vmss
# Note: Depending on your update policy, you might need to run Update-AzVmssInstance for each instance

取得虛擬機擴展集已安裝的擴充功能清單:

Get-AzResource -ResourceId "/subscriptions/<mySubscriptionId>/resourceGroups/<myResourceGroup>/providers/Microsoft.Compute/virtualMachineScaleSets/<myVmssName>/extensions"

從虛擬機擴展集卸載應用程式監視擴充功能:

# set resource group and vmss name to reflect your environment
$vmss = Get-AzVmss -ResourceGroupName "<myResourceGroup>" -VMScaleSetName "<myVmssName>"
Remove-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "ApplicationMonitoringWindows"
Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -Name $vmss.Name -VirtualMachineScaleSet $vmss
# Note: Depending on your update policy, you might need to run Update-AzVmssInstance for each instance

疑難排解

針對在 Azure 虛擬機和虛擬機擴展集上執行的 .NET 應用程式,尋找 Application Insights 監視代理程式延伸模組的疑難解答秘訣。

如果您在部署擴充功能時遇到問題,請檢閱記錄到下列目錄中找到的檔案的執行輸出:

C:\WindowsAzure\Logs\Plugins\Microsoft.Azure.Diagnostics.ApplicationMonitoringWindows\<version>\

如果您的延伸模組部署成功,但無法看到遙測,可能是代理程式疑難解答涵蓋的下列其中一個問題:

  • 應用程式 bin 目錄中衝突的 DLL
  • 與 IIS 共用設定衝突

測試應用程式主機與擷取服務之間的連線

應用程式深入剖析 SDK 和代理程式會傳送遙測,以擷取為 REST 呼叫擷取到我們擷取的端點。 您可以使用來自 PowerShell 或 curl 命令的原始 REST 用戶端,測試從 Web 伺服器或應用程式主機電腦到擷取服務端點的連線。 請參閱針對 Azure 監視器 Application Insights 中遺失的應用程式遙測進行疑難排解

版本資訊

2.8.44

  • 將 Application Insights .NET/.NET Core SDK 更新為 2.20.1 - 紅色字段。
  • 已啟用 SQL 查詢集合。
  • 已啟用 Microsoft Entra 驗證的支援。

2.8.42

已將 Application Insights .NET/.NET Core SDK 更新為 2.18.1 - 紅色字段。

2.8.41

已新增 ASP.NET Core 自動結構功能。

下一步