|
2 | 2 | title: Event counters in Application Insights | Microsoft Docs
|
3 | 3 | description: Monitor system and custom .NET/.NET Core EventCounters in Application Insights.
|
4 | 4 | ms.topic: conceptual
|
5 |
| -ms.date: 09/20/2019 |
| 5 | +ms.date: 07/21/2023 |
6 | 6 | ms.custom: devx-track-csharp, devx-track-dotnet
|
7 |
| -ms.reviewer: cithomas |
| 7 | +ms.reviewer: mmcc |
8 | 8 | ---
|
9 | 9 |
|
10 | 10 | # EventCounters introduction
|
11 | 11 |
|
12 | 12 | [`EventCounter`](/dotnet/core/diagnostics/event-counters) is .NET/.NET Core mechanism to publish and consume counters or statistics. EventCounters are supported in all OS platforms - Windows, Linux, and macOS. It can be thought of as a cross-platform equivalent for the [PerformanceCounters](/dotnet/api/system.diagnostics.performancecounter) that is only supported in Windows systems.
|
13 | 13 |
|
14 |
| -While users can publish any custom `EventCounters` to meet their needs, [.NET](/dotnet/fundamentals/) publishes a set of these counters by default. This document will walk through the steps required to collect and view `EventCounters` (system defined or user defined) in Azure Application Insights. |
| 14 | +While users can publish any custom `EventCounters` to meet their needs, [.NET](/dotnet/fundamentals/) publishes a set of these counters by default. This document walks through the steps required to collect and view `EventCounters` (system defined or user defined) in Azure Application Insights. |
15 | 15 |
|
16 | 16 | ## Using Application Insights to collect EventCounters
|
17 | 17 |
|
18 |
| -Application Insights supports collecting `EventCounters` with its `EventCounterCollectionModule`, which is part of the newly released NuGet package [Microsoft.ApplicationInsights.EventCounterCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.EventCounterCollector). `EventCounterCollectionModule` is automatically enabled when using either [AspNetCore](asp-net-core.md) or [WorkerService](worker-service.md). `EventCounterCollectionModule` collects counters with a non-configurable collection frequency of 60 seconds. There are no special permissions required to collect EventCounters. |
| 18 | +Application Insights supports collecting `EventCounters` with its `EventCounterCollectionModule`, which is part of the newly released NuGet package [Microsoft.ApplicationInsights.EventCounterCollector](https://www.nuget.org/packages/Microsoft.ApplicationInsights.EventCounterCollector). `EventCounterCollectionModule` is automatically enabled when using either [AspNetCore](asp-net-core.md) or [WorkerService](worker-service.md). `EventCounterCollectionModule` collects counters with a nonconfigurable collection frequency of 60 seconds. There are no special permissions required to collect EventCounters. For ASP.NET Core applications, you also want to add the [Microsoft.ApplicationInsights.AspNetCore](https://www.nuget.org/packages/Microsoft.ApplicationInsights.AspNetCore) package. |
| 19 | + |
| 20 | +```dotnetcli |
| 21 | +dotnet add package Microsoft.ApplicationInsights.EventCounterCollector |
| 22 | +dotnet add package Microsoft.ApplicationInsights.AspNetCore |
| 23 | +``` |
19 | 24 |
|
20 | 25 | ## Default counters collected
|
21 | 26 |
|
22 |
| -Starting with 2.15.0 version of either [AspNetCore SDK](asp-net-core.md) or [WorkerService SDK](worker-service.md), no counters are collected by default. The module itself is enabled, so users can simply add the desired counters to |
| 27 | +Starting with 2.15.0 version of either [AspNetCore SDK](asp-net-core.md) or [WorkerService SDK](worker-service.md), no counters are collected by default. The module itself is enabled, so users can add the desired counters to |
23 | 28 | collect them.
|
24 | 29 |
|
25 | 30 | To get a list of well known counters published by the .NET Runtime, see [Available Counters](/dotnet/core/diagnostics/event-counters#available-counters) document.
|
26 | 31 |
|
27 | 32 | ## Customizing counters to be collected
|
28 | 33 |
|
29 |
| -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](worker-service.md#configure-or-remove-default-telemetry-modules) document. |
| 34 | +The following example shows how to add/remove counters. This customization would be done as part of your application service configuration 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](worker-service.md#configure-or-remove-default-telemetry-modules) document. |
| 35 | + |
| 36 | + |
| 37 | +# [ASP.NET Core 6.0+](#tab/dotnet6) |
| 38 | + |
| 39 | +```csharp |
| 40 | +using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector; |
| 41 | +using Microsoft.Extensions.DependencyInjection; |
| 42 | + |
| 43 | +builder.Services.ConfigureTelemetryModule<EventCounterCollectionModule>( |
| 44 | + (module, o) => |
| 45 | + { |
| 46 | + // Removes all default counters, if any. |
| 47 | + module.Counters.Clear(); |
| 48 | + |
| 49 | + // Adds a user defined counter "MyCounter" from EventSource named "MyEventSource" |
| 50 | + module.Counters.Add( |
| 51 | + new EventCounterCollectionRequest("MyEventSource", "MyCounter")); |
| 52 | + |
| 53 | + // Adds the system counter "gen-0-size" from "System.Runtime" |
| 54 | + module.Counters.Add( |
| 55 | + new EventCounterCollectionRequest("System.Runtime", "gen-0-size")); |
| 56 | + } |
| 57 | + ); |
| 58 | +``` |
| 59 | + |
| 60 | +# [ASP.NET Core 3.1](#tab/dotnet31) |
30 | 61 |
|
31 | 62 | ```csharp
|
32 |
| - using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector; |
33 |
| - using Microsoft.Extensions.DependencyInjection; |
34 |
| - |
35 |
| - public void ConfigureServices(IServiceCollection services) |
36 |
| - { |
37 |
| - //... other code... |
38 |
| -
|
39 |
| - // The following code shows how to configure the module to collect |
40 |
| - // additional counters. |
41 |
| - services.ConfigureTelemetryModule<EventCounterCollectionModule>( |
42 |
| - (module, o) => |
43 |
| - { |
44 |
| - // This removes all default counters, if any. |
45 |
| - module.Counters.Clear(); |
46 |
| - |
47 |
| - // This adds a user defined counter "MyCounter" from EventSource named "MyEventSource" |
48 |
| - module.Counters.Add(new EventCounterCollectionRequest("MyEventSource", "MyCounter")); |
49 |
| - |
50 |
| - // This adds the system counter "gen-0-size" from "System.Runtime" |
51 |
| - module.Counters.Add(new EventCounterCollectionRequest("System.Runtime", "gen-0-size")); |
52 |
| - } |
53 |
| - ); |
54 |
| - } |
| 63 | +using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector; |
| 64 | +using Microsoft.Extensions.DependencyInjection; |
| 65 | + |
| 66 | +public void ConfigureServices(IServiceCollection services) |
| 67 | +{ |
| 68 | + //... other code... |
| 69 | +
|
| 70 | + // The following code shows how to configure the module to collect |
| 71 | + // additional counters. |
| 72 | + services.ConfigureTelemetryModule<EventCounterCollectionModule>( |
| 73 | + (module, o) => |
| 74 | + { |
| 75 | + // Removes all default counters, if any. |
| 76 | + module.Counters.Clear(); |
| 77 | + |
| 78 | + // Adds a user defined counter "MyCounter" from EventSource named "MyEventSource" |
| 79 | + module.Counters.Add( |
| 80 | + new EventCounterCollectionRequest("MyEventSource", "MyCounter")); |
| 81 | + |
| 82 | + // Adds the system counter "gen-0-size" from "System.Runtime" |
| 83 | + module.Counters.Add( |
| 84 | + new EventCounterCollectionRequest("System.Runtime", "gen-0-size")); |
| 85 | + } |
| 86 | + ); |
| 87 | +} |
55 | 88 | ```
|
56 | 89 |
|
| 90 | +--- |
| 91 | + |
57 | 92 | ## Disabling EventCounter collection module
|
58 | 93 |
|
59 |
| -`EventCounterCollectionModule` can be disabled by using `ApplicationInsightsServiceOptions`. An |
60 |
| -example when using ASP.NET Core SDK is shown below. |
| 94 | +`EventCounterCollectionModule` can be disabled by using `ApplicationInsightsServiceOptions`. |
| 95 | + |
| 96 | +The following example uses the ASP.NET Core SDK. |
| 97 | + |
| 98 | +# [ASP.NET Core 6.0+](#tab/dotnet6) |
| 99 | + |
| 100 | +```csharp |
| 101 | +using Microsoft.ApplicationInsights.AspNetCore.Extensions; |
| 102 | +using Microsoft.Extensions.DependencyInjection; |
| 103 | + |
| 104 | +var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions(); |
| 105 | +applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false; |
| 106 | +builder.Services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions); |
| 107 | +``` |
| 108 | + |
| 109 | +# [ASP.NET Core 3.1](#tab/dotnet31) |
61 | 110 |
|
62 | 111 | ```csharp
|
63 |
| - using Microsoft.ApplicationInsights.AspNetCore.Extensions; |
64 |
| - using Microsoft.Extensions.DependencyInjection; |
| 112 | +using Microsoft.ApplicationInsights.AspNetCore.Extensions; |
| 113 | +using Microsoft.Extensions.DependencyInjection; |
65 | 114 |
|
66 |
| - public void ConfigureServices(IServiceCollection services) |
67 |
| - { |
68 |
| - //... other code... |
| 115 | +public void ConfigureServices(IServiceCollection services) |
| 116 | +{ |
| 117 | + //... other code... |
69 | 118 |
|
70 |
| - var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions(); |
71 |
| - applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false; |
72 |
| - services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions); |
73 |
| - } |
| 119 | + var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions(); |
| 120 | + applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false; |
| 121 | + services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions); |
| 122 | +} |
74 | 123 | ```
|
75 | 124 |
|
76 |
| -A similar approach can be used for the WorkerService SDK as well, but the namespace must be |
77 |
| -changed as shown in the example below. |
| 125 | +--- |
| 126 | + |
| 127 | +A similar approach can be used for the WorkerService SDK as well, but the namespace must be changed as shown in the following example. |
| 128 | + |
| 129 | +# [ASP.NET Core 6.0+](#tab/dotnet6) |
78 | 130 |
|
79 | 131 | ```csharp
|
80 |
| - using Microsoft.ApplicationInsights.WorkerService; |
81 |
| - using Microsoft.Extensions.DependencyInjection; |
| 132 | +using Microsoft.ApplicationInsights.AspNetCore.Extensions; |
| 133 | +using Microsoft.Extensions.DependencyInjection; |
| 134 | + |
| 135 | +var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions(); |
| 136 | +applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false; |
| 137 | +builder.Services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions); |
| 138 | +``` |
| 139 | + |
| 140 | +# [ASP.NET Core 3.1](#tab/dotnet31) |
| 141 | + |
| 142 | +```csharp |
| 143 | +using Microsoft.ApplicationInsights.WorkerService; |
| 144 | +using Microsoft.Extensions.DependencyInjection; |
| 145 | + |
| 146 | +public void ConfigureServices(IServiceCollection services) |
| 147 | +{ |
| 148 | + //... other code... |
82 | 149 |
|
83 | 150 | var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions();
|
84 | 151 | applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false;
|
85 | 152 | services.AddApplicationInsightsTelemetryWorkerService(applicationInsightsServiceOptions);
|
| 153 | +} |
86 | 154 | ```
|
87 | 155 |
|
| 156 | +--- |
| 157 | + |
88 | 158 | ## Event counters in Metric Explorer
|
89 | 159 |
|
90 | 160 | To view EventCounter metrics in [Metric Explorer](../essentials/metrics-charts.md), select Application Insights resource, and chose Log-based metrics as metric namespace. Then EventCounter metrics get displayed under Custom category.
|
@@ -120,18 +190,18 @@ customMetrics
|
120 | 190 | 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.
|
121 | 191 |
|
122 | 192 | ## Alerts
|
123 |
| -Like other metrics, you can [set an alert](../alerts/alerts-log.md) to warn you if an event counter goes outside a limit you specify. Open the Alerts pane and click Add Alert. |
| 193 | +Like other metrics, you can [set an alert](../alerts/alerts-log.md) to warn you if an event counter goes outside a limit you specify. Open the Alerts pane and select Add Alert. |
124 | 194 |
|
125 | 195 | ## Frequently asked questions
|
126 | 196 |
|
127 | 197 | ### Can I see EventCounters in Live Metrics?
|
128 | 198 |
|
129 |
| -Live Metrics do not show EventCounters as of today. Use Metric Explorer or Analytics to see the telemetry. |
| 199 | +Live Metrics don't show EventCounters as of today. Use Metric Explorer or Analytics to see the telemetry. |
130 | 200 |
|
131 |
| -### I have enabled Application Insights from Azure Web App Portal. But I can't see EventCounters.? |
| 201 | +### I have enabled Application Insights from Azure Web App Portal. Why can't I see EventCounters? |
132 | 202 |
|
133 |
| - [Application Insights extension](./azure-web-apps.md) for ASP.NET Core doesn't yet support this feature. This document will be updated when this feature is supported. |
| 203 | + [Application Insights extension](./azure-web-apps.md) for ASP.NET Core doesn't yet support this feature. |
134 | 204 |
|
135 | 205 | ## <a name="next"></a>Next steps
|
136 | 206 |
|
137 |
| -* [Dependency tracking](./asp-net-dependencies.md) |
| 207 | +* [Dependency tracking](./asp-net-dependencies.md) |
0 commit comments