共用方式為


從 Application Insights Classic API SDK 遷移到 Azure Monitor OpenTelemetry

本指南提供逐步指引,教你如何將應用程式從使用 Application Insights SDK(Classic API)遷移到 Azure Monitor OpenTelemetry。

預期使用 Azure Monitor OpenTelemetry 的工具體驗將與使用 Application Insights SDK 的體驗相似。 欲了解更多資訊及逐項功能比較,請參閱 功能發布狀態

使用 Application Insights .NET 軟體開發套件(SDK)3.x,從 Application Insights .NET SDK 2.x 升級為基於 OpenTelemetry(OTel)的實作。 3.x SDK 保留大部分TelemetryClientTelemetryConfiguration應用程式介面(API),並使用 Azure Monitor OpenTelemetry 匯出器將遙測資料傳送給 Application Insights。

如果你正在建立新應用程式,或已經在使用 Azure Monitor OpenTelemetry 發行版,建議改用 Azure Monitor OpenTelemetry 發行 版。 不要在同一個應用程式中使用 Application Insights .NET SDK 3.x 和 Azure Monitor OpenTelemetry 發行版。

Application Insights .NET SDK 3.x 概覽

Application Insights .NET SDK 3.x 提供以下 NuGet 套件:

  • Microsoft.ApplicationInsights 用於 TelemetryClientTelemetryConfiguration
  • Microsoft.ApplicationInsights.AspNetCore 用於 ASP.NET (Active Server Pages .NET) Core Web 應用程式
  • Microsoft.ApplicationInsights.WorkerService 用於 Worker Service 與控制台應用程式
  • Microsoft.ApplicationInsights.Web 用於 .NET Framework 上的 ASP.NET 應用程式
  • Microsoft.ApplicationInsights.NLogTarget 用於 NLog 整合(beta)

請參考資料庫文件以查看程式碼範例及 OpenTelemetry 整合細節:

升級到 3.x 版本

步驟 1:移除對不相容套件的引用

移除這些套件,因為它們與 SDK 3.x 不相容:

  • Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel
  • Microsoft.ApplicationInsights.DependencyCollector
  • Microsoft.ApplicationInsights.EventCounterCollector
  • Microsoft.ApplicationInsights.PerfCounterCollector
  • Microsoft.ApplicationInsights.WindowsServer
  • Microsoft.Extensions.Logging.ApplicationInsights
  • Microsoft.ApplicationInsights.Log4NetAppender
  • Microsoft.ApplicationInsights.TraceListener
  • Microsoft.ApplicationInsights.DiagnosticSourceListener
  • Microsoft.ApplicationInsights.EtwCollector
  • Microsoft.ApplicationInsights.EventSourceListener

SDK 3.x 不會發布這些套件的 3.x 版本。 建議改用 Application Insights .NET SDK 3.x 總覽 中列出的支援的 3.x 套件。

步驟 2:將套件版本升級到 3.x

將所有剩餘支援的 Application Insights 套件升級至最新版本 3.x。

這很重要

不要在同一個應用程式裡混用 Application Insights 2.x 和 3.x 套件。 將所有的 Application Insights 套件引用同時升級。

步驟 3:更新程式碼與設定以應對破壞性變更

檢視破壞性變更的參考資料,移除或替換不再支援的 API 與設定。

最常見的變更包括:

  • 刪除 TrackPageView 呼叫。
  • 更新 Track* 呼叫以移除自訂的度量參數。
  • 使用完整連接串 TelemetryConfiguration.ConnectionString替換儀表鍵配置。
  • 以 OpenTelemetry 處理器、儀器函式庫及資源偵測器取代 TelemetryModuleTelemetryInitializerTelemetryProcessor 的自訂功能。 該 ApplicationInsightsServiceOptions 類別包含 EnableQuickPulseMetricStreamEnablePerformanceCounterCollectionModuleEnableDependencyTrackingTelemetryModuleEnableRequestTrackingTelemetryModule。 這些 ApplicationInsightsServiceOptions 設定是用來設定匯出器的行為,並不會使用 TelemetryModule 實作。
  • 將自適應抽樣(EnableAdaptiveSampling)替換為 TracesPerSecondSamplingRatio
  • 請將使用 Microsoft.ApplicationInsights.Web 的 ASP.NET 應用程式鎖定為 .NET Framework 4.6.2 或更新版本。

替換已移除的可擴展點

Application Insights .NET SDK 2.x 提供針對 Application Insights 的可擴充類型,如遙測模組、初始化器與處理器。 Application Insights .NET SDK 3.x 則使用 OpenTelemetry 擴充性。

  • 使用 OpenTelemetry 的儀器與設定選項來控制自動收集。
  • 使用 OpenTelemetry 處理器來豐富或過濾遙測數據。

SDK 3.x 只保留部分 TelemetryContext 屬性。 你可以在個別遙測項目上設定以下屬性:

上下文 屬性
User IdAuthenticatedUserIdUserAgent
Operation Name
Location Ip
GlobalProperties (字典)

設定取樣

Application Insights .NET SDK 3.x 支援兩種追蹤取樣模式(請求與依賴):

  • 設定 SamplingRatio 為百分比抽樣的範圍(0.0 到 1.0)。
  • 設定 TracesPerSecond 為速率限制取樣(預設:每秒五次追蹤)。

SDK 3.x 對請求和相依性套用相同的取樣設定。 SDK 3.x 不支援針對請求和依賴的獨立取樣設定。

當請求或相依性被取樣時,SDK 3.x 預設會將父追蹤的取樣決策套用到相關日誌。 要停用該行為,請設 EnableTraceBasedLogsSamplerfalse

你可以在 SamplingRatioTracesPerSecondEnableTraceBasedLogsSampler 中設定 TelemetryConfigurationappsettings.jsonapplicationinsights.config

疑難排解升級

在升級到 SDK 3.x 期間,請使用以下步驟驗證遙測數據:

  • 收集 Application Insights 自我診斷日誌,以識別設定錯誤與匯出器故障。
  • 新增 OpenTelemetry 主控台匯出器,在您依賴 Azure 監視器內嵌之前,先驗證追蹤、指標與記錄是否如預期發出。
  • 透過驗證父子追蹤決策,確認抽樣設定是否如預期運作。
  • 驗證資源屬性,如服務名稱、角色名稱及環境,以確保應用程式洞察中的正確歸因。

如需詳細的故障排除指引與範例,請參考以下資源: