EventCounter 簡介

EventCounter 是 .NET/.NET Core 機制,用來發佈和取用計數器或統計資料。 所有作業系統平台 (Windows、Linux 和 macOS) 都支援 EventCounter。 您可以將其視為只有在 Windows 系統中才支援的 PerformanceCounter 的跨平台對等項目。

雖然使用者可以發佈任何自訂 EventCounters 以符合其需求,但 .NET 預設會發佈一組這些計數器。 此文件將逐步解說在 Azure Application Insights 中收集及檢視 EventCounters (系統定義或使用者定義) 所需的步驟。

注意

下列文件以 Application Insights 傳統 API 為依據。 Application Insights 的長期計劃是使用 OpenTelemetry 收集資料。 如需詳細資訊,請參閱為 .NET、Node.js、Python 和 Java 應用程式啟用 Azure 監視器 OpenTelemetry

使用 Application Insights 收集 EventCounter

Application Insights 支援以其 EventCounterCollectionModule 收集 EventCounters,這是新發行 NuGet 套件 Microsoft.ApplicationInsights.EventCounterCollector 的一部分。 使用 AspNetCoreWorkerService 時,會自動啟用 EventCounterCollectionModuleEventCounterCollectionModule 會以 60 秒的不可設定的收集頻率收集計數器。 收集 EventCounter 並不需要特殊的權限。 針對 ASP.NET Core 應用程式,您也想要新增 Microsoft.ApplicationInsights.AspNetCore 套件。

dotnet add package Microsoft.ApplicationInsights.EventCounterCollector
dotnet add package Microsoft.ApplicationInsights.AspNetCore

收集的預設計數器

從 2.15.0 版 AspNetCore SDKWorkerService SDK 開始,預設不會收集任何計數器。 模組本身已啟用,因此使用者可以新增所需的計數器來收集這些計數器。

若要取得 .NET 執行時間所發佈的已知計數器清單,請參閱可用的計數器文件。

自訂要收集的計數器

下列範例示範如何新增/移除計數器。 使用 AddApplicationInsightsTelemetry()AddApplicationInsightsWorkerService() 啟用 Application Insights 遙測收集之後,就會在應用程式服務設定中完成此自訂。 以下是來自 ASP.NET Core 應用程式的範例程式碼。 如需其他類型的應用程式,請參閱文件。

using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector;
using Microsoft.Extensions.DependencyInjection;

builder.Services.ConfigureTelemetryModule<EventCounterCollectionModule>(
        (module, o) =>
        {
            // Removes all default counters, if any.
            module.Counters.Clear();

            // Adds a user defined counter "MyCounter" from EventSource named "MyEventSource"
            module.Counters.Add(
                new EventCounterCollectionRequest("MyEventSource", "MyCounter"));

            // Adds the system counter "gen-0-size" from "System.Runtime"
            module.Counters.Add(
                new EventCounterCollectionRequest("System.Runtime", "gen-0-size"));
        }
    );

停用 EventCounter 集合模組

可以使用 ApplicationInsightsServiceOptions 來停用 EventCounterCollectionModule

下列範例使用 ASP.NET Core SDK。

using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.Extensions.DependencyInjection;

var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions();
applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false;
builder.Services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions);

同樣地,背景工作角色服務 SDK 也可以使用類似的方法,但命名空間必須變更,如下列範例所示。

using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.Extensions.DependencyInjection;

var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions();
applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false;
builder.Services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions);

計量瀏覽器中的事件計數器

若要在計量瀏覽器中檢視 EventCounter 計量,請選取 Application Insights 資源,然後選擇 [記錄型計量] 作為計量命名空間。 接著,EventCounter 計量會顯示在 [自訂] 類別之下。

Event counters reported in Application Insights Metric Explorer

Analytics 中的事件計數器

您也可以在 AnalyticscustomMetrics 資料表中,搜尋並顯示事件計數器報告。

例如,執行下列查詢以查看已收集哪些計數器並可供查詢:

customMetrics | summarize avg(value) by name

Event counters reported in Application Insights Analytics

若要取得最近一段時間內特定計數器的圖表 (例如:ThreadPool Completed Work Item Count),請執行下列查詢。

customMetrics 
| where name contains "System.Runtime|ThreadPool Completed Work Item Count"
| where timestamp >= ago(1h)
| summarize  avg(value) by cloud_RoleInstance, bin(timestamp, 1m)
| render timechart

Chat of a single counter in Application Insights

與其他遙測一樣,customMetrics 也有 cloud_RoleInstance 資料行,可指出應用程式執行所在主機伺服器執行個體的身分識別。 上述查詢會顯示每個執行個體的計數器值,而且可以用來比較不同伺服器執行個體的效能。

警示

與其他計量一樣,您可以設定警示,在事件計數器超出您指定的界限時提出警告。 開啟 [警示] 窗格,然後選取 [新增警示]。

常見問題集

我是否可以在即時計量中看到 EventCounter?

目前為止,即時計量不會顯示 EventCounter。 請使用 [計量瀏覽器] 或 Analytics 查看遙測。

我已從 Azure Web 應用程式入口網站啟用 Application Insights。 為什麼我看不到 EventCounter?

ASP.NET Core 的 Application Insights 延伸模組尚不支援此功能。

下一步