Skip to content

Commit d91eafd

Browse files
authored
Merge pull request #218878 from kainawroth/patch-3
Adding .NET 6 version of collecting performance counters
2 parents 0ad1d8a + 83e98be commit d91eafd

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

articles/azure-monitor/app/performance-counters.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,50 @@ Or you can do the same thing with custom metrics that you created:
112112
113113
### Collect performance counters in code for ASP.NET Core web applications
114114
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`:
116118
117119
```csharp
118120
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector;
119121
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) =>
121129
{
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+
}
132155
```
133156
157+
---
158+
134159
## Performance counters in Log Analytics
135160
You can search and display performance counter reports in [Log Analytics](../logs/log-query-overview.md).
136161

0 commit comments

Comments
 (0)