EventCounters introduction

EventCounter is .NET/.NET Core mechanism to publish and consume counters or statistics. This document gives an overview of EventCounters and examples on how to publish and consume them. EventCounters are supported in all OS platforms - Windows, Linux, and macOS. It can be thought of as a cross-platform equivalent for the PerformanceCounters that is only supported in Windows systems.

While users can publish any custom EventCounters to meet their needs, the .NET Core 3.0 runtime publishes a set of these counters by default. The document will walk through the steps required to collect and view EventCounters (system defined or user defined) in Azure Application Insights.

Using Application Insights to collect EventCounters

Application Insights supports collecting EventCounters with its EventCounterCollectionModule, which is part of the newly released nuget package Microsoft.ApplicationInsights.EventCounterCollector. EventCounterCollectionModule is automatically enabled when using either AspNetCore or WorkerService. EventCounterCollectionModule collects counters with a non-configurable collection frequency of 60 seconds. There are no special permissions required to collect EventCounters.

Default counters collected

For apps running in .NET Core 3.0, the following counters are collected automatically by the SDK. The name of the counters will be of the form "Category|Counter".

Category Counter
System.Runtime cpu-usage
System.Runtime working-set
System.Runtime gc-heap-size
System.Runtime gen-0-gc-count
System.Runtime gen-1-gc-count
System.Runtime gen-2-gc-count
System.Runtime time-in-gc
System.Runtime gen-0-size
System.Runtime gen-1-size
System.Runtime gen-2-size
System.Runtime loh-size
System.Runtime alloc-rate
System.Runtime assembly-count
System.Runtime exception-count
System.Runtime threadpool-thread-count
System.Runtime monitor-lock-contention-count
System.Runtime threadpool-queue-length
System.Runtime threadpool-completed-items-count
System.Runtime active-timer-count
Microsoft.AspNetCore.Hosting requests-per-second
Microsoft.AspNetCore.Hosting total-requests
Microsoft.AspNetCore.Hosting current-requests
Microsoft.AspNetCore.Hosting failed-requests

Note

Counters of category Microsoft.AspNetCore.Hosting are only added in ASP.NET Core Applications.

Customizing counters to be collected

The following example shows how to add/remove counters. This customization would be done in the ConfigureServices method of your application after Application Insights telemetry collection is enabled using either AddApplicationInsightsTelemetry() or AddApplicationInsightsWorkerService(). 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;

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

        // The following code shows several customizations done to EventCounterCollectionModule.
        services.ConfigureTelemetryModule<EventCounterCollectionModule>(
            (module, o) =>
            {
                // This removes all default counters.
                module.Counters.Clear();

                // 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"));
            }
        );

        // The following code removes EventCounterCollectionModule to disable the module completely.
        var eventCounterModule = services.FirstOrDefault<ServiceDescriptor>
                    (t => t.ImplementationType == typeof(EventCounterCollectionModule));
        if (eventCounterModule != null)
        {
            services.Remove(eventCounterModule);
        }
    }

Event counters in Metric Explorer

To view EventCounter metrics in Metric Explorer, select Application Insights resource, and chose Log-based metrics as metric namespace. Then EventCounter metrics get displayed under Custom category.

Event counters reported in Application Insights

Event counters in Analytics

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

Event counters reported in Application Insights

To get a chart of a specific counter (for example: ThreadPool Completed Work Item Count) over the recent period, run the following query.

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

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.

Alerts

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

Can I see EventCounters in Live Metrics?

Live Metrics do not show EventCounters as of today. Use Metric Explorer or Analytics to see the telemetry.

Which platforms can I see the default list of .NET Core 3.0 counters?

EventCounter doesn't require any special permissions, and is supported in all platforms .NET Core 3.0 is supported. This includes:

  • Operating system: Windows, Linux, or macOS.
  • Hosting method: In process or out of process.
  • Deployment method: Framework dependent or self-contained.
  • Web server: IIS (Internet Information Server) or Kestrel.
  • Hosting platform: The Web Apps feature of Azure App Service, Azure VM, Docker, Azure Kubernetes Service (AKS), and so on.

I have enabled Application Insights from Azure Web App Portal. But I can't see EventCounters.?

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