EventCounters 简介EventCounters introduction

EventCounter 用于发布和使用计数器或统计信息的 .NET/.NET Core 机制。EventCounter is .NET/.NET Core mechanism to publish and consume counters or statistics. 所有 OS 平台(Windows、Linux 和 macOS)都支持 EventCounters。EventCounters are supported in all OS platforms - Windows, Linux, and macOS. 可以将其视为仅在 Windows 系统中受支持的 PerformanceCounters 的等效跨平台。It can be thought of as a cross-platform equivalent for the PerformanceCounters that is only supported in Windows systems.

尽管用户可以根据需要发布任何自定义 EventCounters,但 .NET Core 3.0 运行时及更高版本默认会发布一组此类计数器。While users can publish any custom EventCounters to meet their needs, .NET Core 3.0 and higher runtime publishes a set of these counters by default. 该文档将指导你完成在 Azure Application Insights 中收集和查看 EventCounters(系统定义的或用户定义的)所需的步骤。This document will walk through the steps required to collect and view EventCounters (system defined or user defined) in Azure Application Insights.

使用 Application Insights 收集 EventCountersUsing Application Insights to collect EventCounters

Application Insights 支持使用 EventCounterCollectionModule 来收集 EventCounters,新发布的 NuGet 包 Microsoft.ApplicationInsights.EventCounterCollector 中包含该模块。Application Insights supports collecting EventCounters with its EventCounterCollectionModule, which is part of the newly released NuGet package Microsoft.ApplicationInsights.EventCounterCollector. 使用 AspNetCoreWorkerService 时,将自动启用 EventCounterCollectionModuleEventCounterCollectionModule is automatically enabled when using either AspNetCore or WorkerService. EventCounterCollectionModule 会以不可配置的收集频率(60 秒)收集计数器。EventCounterCollectionModule collects counters with a non-configurable collection frequency of 60 seconds. 收集 EventCounters 时,不需要具备特殊的权限。There are no special permissions required to collect EventCounters.

已收集默认计数器Default counters collected

AspNetCore SDKWorkerService SDK 的 2.15.0 版本开始,默认情况下不收集任何计数器。Starting with 2.15.0 version of either AspNetCore SDK or WorkerService SDK, no counters are collected by default. 模块本身已被启用,因此用户只需想向其添加所需的计数器即可收集它们。The module itself is enabled, so users can simply add the desired counters to collect them.

若要获取由 .NET 运行时发布的常用计数器列表,请参阅可用计数器文档。To get a list of well known counters published by the .NET Runtime, see Available Counters document.

自定义要收集的计数器Customizing counters to be collected

以下示例介绍如何添加/删除计数器。The following example shows how to add/remove counters. 使用 AddApplicationInsightsTelemetry()AddApplicationInsightsWorkerService() 启用 Application Insights 遥测收集后,将在应用程序的 ConfigureServices 方法中完成此自定义。This customization would be done in the ConfigureServices method of your application after Application Insights telemetry collection is enabled using either AddApplicationInsightsTelemetry() or AddApplicationInsightsWorkerService(). 下面是 ASP.NET Core 应用程序的示例代码。Following is an example code from an ASP.NET Core application. 对于其他类型的应用程序,请参阅此文档For other type of applications, refer to this document.

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

    public void ConfigureServices(IServiceCollection services)
        //... other code...

        // The following code shows how to configure the module to collect
        // additional counters.
            (module, o) =>
                // This removes all default counters, if any.

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

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

禁用 EventCounter 收集模块Disabling EventCounter collection module

可以使用 ApplicationInsightsServiceOptions 禁用 EventCounterCollectionModuleEventCounterCollectionModule can be disabled by using ApplicationInsightsServiceOptions. 下面显示了使用 ASP.NET Core SDK 时的示例。An example when using ASP.NET Core SDK is shown below.

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

    public void ConfigureServices(IServiceCollection services)
        //... other code...

        var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions();
        applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false;

类似的方法也可用于 WorkerService SDK,但必须更改命名空间,如以下示例中所示。A similar approach can be used for the WorkerService SDK as well, but the namespace must be changed as shown in the example below.

    using Microsoft.ApplicationInsights.WorkerService;
    using Microsoft.Extensions.DependencyInjection;

    var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions();
    applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false;

Metric Explorer 中的事件计数器Event counters in Metric Explorer

若要在 Metric Explorer 中查看 EventCounter 指标,请选择 Application Insights 资源,然后选择基于日志的指标作为指标命名空间。To view EventCounter metrics in Metric Explorer, select Application Insights resource, and chose Log-based metrics as metric namespace. EventCounter 指标随即显示在“自定义”类别下。Then EventCounter metrics get displayed under Custom category.

Application Insights 指标资源管理器中报告的事件计数器Event counters reported in Application Insights Metric Explorer

Analytics 中的事件计数器Event counters in Analytics

还可以在 Analytics 的“customMetrics”表中搜索和显示事件计数器报表。You can also search and display event counter reports in Analytics, in the customMetrics table.

例如,运行以下查询,以查看收集了哪些计数器并可用于查询:For example, run the following query to see what counters are collected and available to query:

customMetrics | summarize avg(value) by name

Application Insights Analytics 中报告的事件计数器Event counters reported in Application Insights Analytics

若要在最近一段时间内获取特定计数器(例如:ThreadPool Completed Work Item Count)的图表,请运行以下查询。To get a chart of a specific counter (for example: ThreadPool Completed Work Item Count) over the recent period, run the following query.

| 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

Application Insights 中的单个计数器的图表Chat of a single counter in Application Insights

与其他遥测一样,customMetrics 同样也具有列 cloud_RoleInstance,指示正在其上运行应用的主机服务器实例的标识。Like other telemetry, customMetrics also has a column cloud_RoleInstance that indicates the identity of the host server instance on which your app is running. 上述查询显示每个实例的计数器值,并可用于比较不同服务器实例的性能。The above query shows the counter value per instance, and can be used to compare performance of different server instances.


与其他指标一样,可以设置警报以便在事件计数器超出指定的限制时收到警报。Like other metrics, you can set an alert to warn you if an event counter goes outside a limit you specify. 打开“警报”窗格,并单击“添加警报”。Open the Alerts pane and click Add Alert.

常见问题Frequently asked questions

是否能在实时指标中看到 EventCounters?Can I see EventCounters in Live Metrics?

实时指标目前不显示 EventCounters。Live Metrics do not show EventCounters as of today. 使用 Metric Explorer 或 Analytics 来查看遥测数据。Use Metric Explorer or Analytics to see the telemetry.

我已从 Azure Web 应用门户启用 Application Insights。I have enabled Application Insights from Azure Web App Portal. 但看不到 EventCounters,这是什么原因?But I can't see EventCounters.?

ASP.NET Core 的 Application Insights 扩展尚不支持此功能。Application Insights extension for ASP.NET Core doesn't yet support this feature. 支持此功能后,本文档会更新。This document will be updated when this feature is supported.

后续步骤Next steps