Skip to content

Commit 379be00

Browse files
authored
Merge pull request #245860 from AaronMaxwell/aaronmax-event-counters-code-sample-update
Updating code samples for the latest .NET
2 parents 7beefae + 2ecbadf commit 379be00

File tree

1 file changed

+119
-49
lines changed

1 file changed

+119
-49
lines changed

articles/azure-monitor/app/eventcounters.md

Lines changed: 119 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,159 @@
22
title: Event counters in Application Insights | Microsoft Docs
33
description: Monitor system and custom .NET/.NET Core EventCounters in Application Insights.
44
ms.topic: conceptual
5-
ms.date: 09/20/2019
5+
ms.date: 07/21/2023
66
ms.custom: devx-track-csharp, devx-track-dotnet
7-
ms.reviewer: cithomas
7+
ms.reviewer: mmcc
88
---
99

1010
# EventCounters introduction
1111

1212
[`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.
1313

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.
1515

1616
## Using Application Insights to collect EventCounters
1717

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+
```
1924

2025
## Default counters collected
2126

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
2328
collect them.
2429

2530
To get a list of well known counters published by the .NET Runtime, see [Available Counters](/dotnet/core/diagnostics/event-counters#available-counters) document.
2631

2732
## Customizing counters to be collected
2833

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)
3061

3162
```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+
}
5588
```
5689

90+
---
91+
5792
## Disabling EventCounter collection module
5893

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)
61110

62111
```csharp
63-
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
64-
using Microsoft.Extensions.DependencyInjection;
112+
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
113+
using Microsoft.Extensions.DependencyInjection;
65114

66-
public void ConfigureServices(IServiceCollection services)
67-
{
68-
//... other code...
115+
public void ConfigureServices(IServiceCollection services)
116+
{
117+
//... other code...
69118
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+
}
74123
```
75124

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)
78130

79131
```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...
82149
83150
var applicationInsightsServiceOptions = new ApplicationInsightsServiceOptions();
84151
applicationInsightsServiceOptions.EnableEventCounterCollectionModule = false;
85152
services.AddApplicationInsightsTelemetryWorkerService(applicationInsightsServiceOptions);
153+
}
86154
```
87155

156+
---
157+
88158
## Event counters in Metric Explorer
89159

90160
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
120190
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.
121191

122192
## 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.
124194

125195
## Frequently asked questions
126196

127197
### Can I see EventCounters in Live Metrics?
128198

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.
130200

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?
132202

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.
134204

135205
## <a name="next"></a>Next steps
136206

137-
* [Dependency tracking](./asp-net-dependencies.md)
207+
* [Dependency tracking](./asp-net-dependencies.md)

0 commit comments

Comments
 (0)