@@ -112,25 +112,50 @@ Or you can do the same thing with custom metrics that you created:
112
112
113
113
### Collect performance counters in code for ASP.NET Core web applications
114
114
115
- Modify the `ConfigureServices` method in your `Startup.cs` class:
115
+ ### [ASP.NET Core 6 and later](#tab/net-core-new)
116
+
117
+ Configure `PerformanceCollectorModule` after the `WebApplication.CreateBuilder()` method in `Program.cs`:
116
118
117
119
```csharp
118
120
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector;
119
121
120
- public void ConfigureServices(IServiceCollection services)
122
+ var builder = WebApplication.CreateBuilder(args);
123
+
124
+ builder.Services.AddApplicationInsightsTelemetry();
125
+
126
+ // The following configures PerformanceCollectorModule.
127
+
128
+ builder.Services.ConfigureTelemetryModule<PerformanceCollectorModule>((module, o) =>
121
129
{
122
- services.AddApplicationInsightsTelemetry();
123
-
124
- // The following configures PerformanceCollectorModule.
125
- services.ConfigureTelemetryModule<PerformanceCollectorModule>((module, o) =>
126
- {
127
- // the application process name could be "dotnet" for ASP.NET Core self-hosted applications.
128
- module.Counters.Add(new PerformanceCounterCollectionRequest(
129
- @"\Process([replace-with-application-process-name])\Page Faults/sec", "DotnetPageFaultsPerfSec"));
130
- });
131
- }
130
+ // The application process name could be "dotnet" for ASP.NET Core self-hosted applications.
131
+ module.Counters.Add(new PerformanceCounterCollectionRequest(@"\Process([replace-with-application-process-name])\Page Faults/sec", "DotnetPageFaultsPerfSec"));
132
+ });
133
+
134
+ var app = builder.Build();
135
+ ```
136
+
137
+ ### [ASP.NET Core 5 and earlier](#tab/net-core-old)
138
+
139
+ Configure `PerformanceCollectorModule` in the `ConfigureServices()` method in `Startup.cs`:
140
+
141
+ ```csharp
142
+ using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector;
143
+
144
+ public void ConfigureServices(IServiceCollection services)
145
+ {
146
+ services.AddApplicationInsightsTelemetry();
147
+
148
+ // The following configures PerformanceCollectorModule:
149
+ services.ConfigureTelemetryModule<PerformanceCollectorModule>((module, o) =>
150
+ {
151
+ // The application process name could be "dotnet" for ASP.NET Core self-hosted applications.
152
+ module.Counters.Add(new PerformanceCounterCollectionRequest(@"\Process([replace-with-application-process-name])\PageFaults/sec", "DotnetPageFaultsPerfSec"));
153
+ });
154
+ }
132
155
```
133
156
157
+ ---
158
+
134
159
## Performance counters in Log Analytics
135
160
You can search and display performance counter reports in [Log Analytics](../logs/log-query-overview.md).
136
161
0 commit comments