@@ -60,61 +60,171 @@ To manually configure Live Metrics:
60
60
1 . Install the NuGet package [ Microsoft.ApplicationInsights.PerfCounterCollector] ( https://www.nuget.org/packages/Microsoft.ApplicationInsights.PerfCounterCollector ) .
61
61
1 . The following sample console app code shows setting up Live Metrics:
62
62
63
- ``` csharp
64
- using Microsoft .ApplicationInsights ;
65
- using Microsoft .ApplicationInsights .Extensibility ;
66
- using Microsoft .ApplicationInsights .Extensibility .PerfCounterCollector .QuickPulse ;
67
- using System ;
68
- using System .Threading .Tasks ;
69
-
70
- namespace LiveMetricsDemo
63
+ # [ .NET 6.0+] ( #tab/dotnet6 )
64
+
65
+ ``` csharp
66
+ using Microsoft .ApplicationInsights ;
67
+ using Microsoft .ApplicationInsights .Extensibility ;
68
+ using Microsoft .ApplicationInsights .Extensibility .PerfCounterCollector .QuickPulse ;
69
+
70
+ // Create a TelemetryConfiguration instance.
71
+ TelemetryConfiguration config = TelemetryConfiguration .CreateDefault ();
72
+ config .InstrumentationKey = " INSTRUMENTATION-KEY-HERE" ;
73
+ QuickPulseTelemetryProcessor quickPulseProcessor = null ;
74
+ config .DefaultTelemetrySink .TelemetryProcessorChainBuilder
75
+ .Use ((next ) =>
76
+ {
77
+ quickPulseProcessor = new QuickPulseTelemetryProcessor (next );
78
+ return quickPulseProcessor ;
79
+ })
80
+ .Build ();
81
+
82
+ var quickPulseModule = new QuickPulseTelemetryModule ();
83
+
84
+ // Secure the control channel.
85
+ // This is optional, but recommended.
86
+ quickPulseModule .AuthenticationApiKey = " YOUR-API-KEY-HERE" ;
87
+ quickPulseModule .Initialize (config );
88
+ quickPulseModule .RegisterTelemetryProcessor (quickPulseProcessor );
89
+
90
+ // Create a TelemetryClient instance. It is important
91
+ // to use the same TelemetryConfiguration here as the one
92
+ // used to set up Live Metrics.
93
+ TelemetryClient client = new TelemetryClient (config );
94
+
95
+ // This sample runs indefinitely. Replace with actual application logic.
96
+ while (true )
97
+ {
98
+ // Send dependency and request telemetry.
99
+ // These will be shown in Live Metrics.
100
+ // CPU/Memory Performance counter is also shown
101
+ // automatically without any additional steps.
102
+ client .TrackDependency (" My dependency" , " target" , " http://sample" ,
103
+ DateTimeOffset .Now , TimeSpan .FromMilliseconds (300 ), true );
104
+ client .TrackRequest (" My Request" , DateTimeOffset .Now ,
105
+ TimeSpan .FromMilliseconds (230 ), " 200" , true );
106
+ Task .Delay (1000 ).Wait ();
107
+ }
108
+ ```
109
+
110
+ # [ .NET 5.0] ( #tab/dotnet5 )
111
+
112
+ ``` csharp
113
+ using Microsoft .ApplicationInsights ;
114
+ using Microsoft .ApplicationInsights .Extensibility ;
115
+ using Microsoft .ApplicationInsights .Extensibility .PerfCounterCollector .QuickPulse ;
116
+ using System ;
117
+ using System .Threading .Tasks ;
118
+
119
+ namespace LiveMetricsDemo
120
+ {
121
+ internal class Program
71
122
{
72
- class Program
123
+ static void Main ( string [] args )
73
124
{
74
- static void Main (string [] args )
125
+ // Create a TelemetryConfiguration instance.
126
+ TelemetryConfiguration config = TelemetryConfiguration .CreateDefault ();
127
+ config .InstrumentationKey = " INSTRUMENTATION-KEY-HERE" ;
128
+ QuickPulseTelemetryProcessor quickPulseProcessor = null ;
129
+ config .DefaultTelemetrySink .TelemetryProcessorChainBuilder
130
+ .Use ((next ) =>
131
+ {
132
+ quickPulseProcessor = new QuickPulseTelemetryProcessor (next );
133
+ return quickPulseProcessor ;
134
+ })
135
+ .Build ();
136
+
137
+ var quickPulseModule = new QuickPulseTelemetryModule ();
138
+
139
+ // Secure the control channel.
140
+ // This is optional, but recommended.
141
+ quickPulseModule .AuthenticationApiKey = " YOUR-API-KEY-HERE" ;
142
+ quickPulseModule .Initialize (config );
143
+ quickPulseModule .RegisterTelemetryProcessor (quickPulseProcessor );
144
+
145
+ // Create a TelemetryClient instance. It is important
146
+ // to use the same TelemetryConfiguration here as the one
147
+ // used to set up Live Metrics.
148
+ TelemetryClient client = new TelemetryClient (config );
149
+
150
+ // This sample runs indefinitely. Replace with actual application logic.
151
+ while (true )
75
152
{
76
- // Create a TelemetryConfiguration instance.
77
- TelemetryConfiguration config = TelemetryConfiguration .CreateDefault ();
78
- config .InstrumentationKey = " INSTRUMENTATION-KEY-HERE" ;
79
- QuickPulseTelemetryProcessor quickPulseProcessor = null ;
80
- config .DefaultTelemetrySink .TelemetryProcessorChainBuilder
81
- .Use ((next ) =>
82
- {
83
- quickPulseProcessor = new QuickPulseTelemetryProcessor (next );
84
- return quickPulseProcessor ;
85
- })
86
- .Build ();
87
-
88
- var quickPulseModule = new QuickPulseTelemetryModule ();
89
-
90
- // Secure the control channel.
91
- // This is optional, but recommended.
92
- quickPulseModule .AuthenticationApiKey = " YOUR-API-KEY-HERE" ;
93
- quickPulseModule .Initialize (config );
94
- quickPulseModule .RegisterTelemetryProcessor (quickPulseProcessor );
95
-
96
- // Create a TelemetryClient instance. It is important
97
- // to use the same TelemetryConfiguration here as the one
98
- // used to set up Live Metrics.
99
- TelemetryClient client = new TelemetryClient (config );
100
-
101
- // This sample runs indefinitely. Replace with actual application logic.
102
- while (true )
153
+ // Send dependency and request telemetry.
154
+ // These will be shown in Live Metrics.
155
+ // CPU/Memory Performance counter is also shown
156
+ // automatically without any additional steps.
157
+ client .TrackDependency (" My dependency" , " target" , " http://sample" ,
158
+ DateTimeOffset .Now , TimeSpan .FromMilliseconds (300 ), true );
159
+ client .TrackRequest (" My Request" , DateTimeOffset .Now ,
160
+ TimeSpan .FromMilliseconds (230 ), " 200" , true );
161
+ Task .Delay (1000 ).Wait ();
162
+ }
163
+ }
164
+ }
165
+ }
166
+
167
+ ```
168
+
169
+ # [ .NET Framework] ( #tab/dotnet-framework )
170
+
171
+ ``` csharp
172
+ using Microsoft .ApplicationInsights ;
173
+ using Microsoft .ApplicationInsights .Extensibility ;
174
+ using Microsoft .ApplicationInsights .Extensibility .PerfCounterCollector .QuickPulse ;
175
+ using System ;
176
+ using System .Threading .Tasks ;
177
+
178
+ namespace LiveMetricsDemo
179
+ {
180
+ class Program
181
+ {
182
+ static void Main (string [] args )
183
+ {
184
+ // Create a TelemetryConfiguration instance.
185
+ TelemetryConfiguration config = TelemetryConfiguration .CreateDefault ();
186
+ config .InstrumentationKey = " INSTRUMENTATION-KEY-HERE" ;
187
+ QuickPulseTelemetryProcessor quickPulseProcessor = null ;
188
+ config .DefaultTelemetrySink .TelemetryProcessorChainBuilder
189
+ .Use ((next ) =>
103
190
{
104
- // Send dependency and request telemetry.
105
- // These will be shown in Live Metrics.
106
- // CPU/Memory Performance counter is also shown
107
- // automatically without any additional steps.
108
- client .TrackDependency (" My dependency" , " target" , " http://sample" ,
109
- DateTimeOffset .Now , TimeSpan .FromMilliseconds (300 ), true );
110
- client .TrackRequest (" My Request" , DateTimeOffset .Now ,
111
- TimeSpan .FromMilliseconds (230 ), " 200" , true );
112
- Task .Delay (1000 ).Wait ();
113
- }
191
+ quickPulseProcessor = new QuickPulseTelemetryProcessor (next );
192
+ return quickPulseProcessor ;
193
+ })
194
+ .Build ();
195
+
196
+ var quickPulseModule = new QuickPulseTelemetryModule ();
197
+
198
+ // Secure the control channel.
199
+ // This is optional, but recommended.
200
+ quickPulseModule .AuthenticationApiKey = " YOUR-API-KEY-HERE" ;
201
+ quickPulseModule .Initialize (config );
202
+ quickPulseModule .RegisterTelemetryProcessor (quickPulseProcessor );
203
+
204
+ // Create a TelemetryClient instance. It is important
205
+ // to use the same TelemetryConfiguration here as the one
206
+ // used to set up Live Metrics.
207
+ TelemetryClient client = new TelemetryClient (config );
208
+
209
+ // This sample runs indefinitely. Replace with actual application logic.
210
+ while (true )
211
+ {
212
+ // Send dependency and request telemetry.
213
+ // These will be shown in Live Metrics.
214
+ // CPU/Memory Performance counter is also shown
215
+ // automatically without any additional steps.
216
+ client .TrackDependency (" My dependency" , " target" , " http://sample" ,
217
+ DateTimeOffset .Now , TimeSpan .FromMilliseconds (300 ), true );
218
+ client .TrackRequest (" My Request" , DateTimeOffset .Now ,
219
+ TimeSpan .FromMilliseconds (230 ), " 200" , true );
220
+ Task .Delay (1000 ).Wait ();
114
221
}
115
222
}
116
223
}
117
- ```
224
+ }
225
+ ```
226
+
227
+ ---
118
228
119
229
The preceding sample is for a console app, but the same code can be used in any .NET applications. If any other telemetry modules are enabled to autocollect telemetry, it's important to ensure that the same configuration used for initializing those modules is used for the Live Metrics module.
120
230
@@ -198,23 +308,24 @@ It's possible to try custom filters without having to set up an authenticated ch
198
308
199
309
You can add an API key to configuration for ASP.NET, ASP.NET Core, WorkerService, and Azure Functions apps.
200
310
201
- #### ASP .NET
311
+ # [ .NET 6.0+ ] ( #tab/dotnet6 )
202
312
203
- In the *applicationinsights . config * file , add ` AuthenticationApiKey ` to ` QuickPulseTelemetryModule ` :
313
+ In the * Program.cs * file, add the following namespace :
204
314
205
- ```xml
206
- <Add Type = "Microsoft .ApplicationInsights .Extensibility .PerfCounterCollector .QuickPulse .QuickPulseTelemetryModule , Microsoft .AI .PerfCounterCollector ">
207
- <AuthenticationApiKey >YOUR -API -KEY -HERE </AuthenticationApiKey >
208
- </Add >
315
+ ``` csharp
316
+ using Microsoft .ApplicationInsights .Extensibility .PerfCounterCollector .QuickPulse ;
209
317
```
210
318
211
- #### ASP . NET Core
319
+ Then add the following service registration:
212
320
213
- For [ASP .NET Core ](./asp -net -core .md ) applications , follow these instructions .
321
+ ``` csharp
322
+ // Existing code which includes services.AddApplicationInsightsTelemetry() to enable Application Insights.
323
+ builder .Services .ConfigureTelemetryModule <QuickPulseTelemetryModule > ((module , o ) => module .AuthenticationApiKey = " YOUR-API-KEY-HERE" );
324
+ ```
214
325
215
- Modify ` ConfigureServices ` of your * Startup . cs * file as shown .
326
+ # [ .NET 5.0 ] ( #tab/dotnet5 )
216
327
217
- Add the following namespace :
328
+ In the * Startup.cs * file, add the following namespace:
218
329
219
330
``` csharp
220
331
using Microsoft .ApplicationInsights .Extensibility .PerfCounterCollector .QuickPulse ;
@@ -230,6 +341,19 @@ public void ConfigureServices(IServiceCollection services)
230
341
}
231
342
```
232
343
344
+ # [ .NET Framework] ( #tab/dotnet-framework )
345
+
346
+ In the * applicationinsights.config* file, add ` AuthenticationApiKey ` to ` QuickPulseTelemetryModule ` :
347
+
348
+ ``` xml
349
+ <Add Type =" Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule, Microsoft.AI.PerfCounterCollector" >
350
+ <AuthenticationApiKey >YOUR-API-KEY-HERE</AuthenticationApiKey >
351
+ </Add >
352
+ ```
353
+
354
+
355
+ ---
356
+
233
357
For more information on how to configure ASP.NET Core applications, see [ Configuring telemetry modules in ASP.NET Core] ( ./asp-net-core.md#configure-or-remove-default-telemetrymodules ) .
234
358
235
359
#### WorkerService
0 commit comments