@@ -60,61 +60,114 @@ 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
+ Console .WriteLine (" Hello, World!" );
71
+
72
+ // Create a TelemetryConfiguration instance.
73
+ TelemetryConfiguration config = TelemetryConfiguration .CreateDefault ();
74
+ config .InstrumentationKey = " INSTRUMENTATION-KEY-HERE" ;
75
+ QuickPulseTelemetryProcessor quickPulseProcessor = null ;
76
+ config .DefaultTelemetrySink .TelemetryProcessorChainBuilder
77
+ .Use ((next ) =>
71
78
{
72
- class Program
79
+ quickPulseProcessor = new QuickPulseTelemetryProcessor (next );
80
+ return quickPulseProcessor ;
81
+ })
82
+ .Build ();
83
+
84
+ var quickPulseModule = new QuickPulseTelemetryModule ();
85
+
86
+ // Secure the control channel.
87
+ // This is optional, but recommended.
88
+ quickPulseModule .AuthenticationApiKey = " YOUR-API-KEY-HERE" ;
89
+ quickPulseModule .Initialize (config );
90
+ quickPulseModule .RegisterTelemetryProcessor (quickPulseProcessor );
91
+
92
+ // Create a TelemetryClient instance. It is important
93
+ // to use the same TelemetryConfiguration here as the one
94
+ // used to set up Live Metrics.
95
+ TelemetryClient client = new TelemetryClient (config );
96
+
97
+ // This sample runs indefinitely. Replace with actual application logic.
98
+ while (true )
99
+ {
100
+ // Send dependency and request telemetry.
101
+ // These will be shown in Live Metrics.
102
+ // CPU/Memory Performance counter is also shown
103
+ // automatically without any additional steps.
104
+ client .TrackDependency (" My dependency" , " target" , " http://sample" ,
105
+ DateTimeOffset .Now , TimeSpan .FromMilliseconds (300 ), true );
106
+ client .TrackRequest (" My Request" , DateTimeOffset .Now ,
107
+ TimeSpan .FromMilliseconds (230 ), " 200" , true );
108
+ Task .Delay (1000 ).Wait ();
109
+ }
110
+ ```
111
+
112
+ # [ .NET Framework] ( #tab/dotnet-framework )
113
+
114
+ ``` csharp
115
+ using Microsoft .ApplicationInsights ;
116
+ using Microsoft .ApplicationInsights .Extensibility ;
117
+ using Microsoft .ApplicationInsights .Extensibility .PerfCounterCollector .QuickPulse ;
118
+ using System ;
119
+ using System .Threading .Tasks ;
120
+
121
+ namespace LiveMetricsDemo
122
+ {
123
+ class Program
124
+ {
125
+ static void Main (string [] args )
73
126
{
74
- static void Main (string [] args )
75
- {
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 )
127
+ // Create a TelemetryConfiguration instance.
128
+ TelemetryConfiguration config = TelemetryConfiguration .CreateDefault ();
129
+ config .InstrumentationKey = " INSTRUMENTATION-KEY-HERE" ;
130
+ QuickPulseTelemetryProcessor quickPulseProcessor = null ;
131
+ config .DefaultTelemetrySink .TelemetryProcessorChainBuilder
132
+ .Use ((next ) =>
103
133
{
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
- }
134
+ quickPulseProcessor = new QuickPulseTelemetryProcessor (next );
135
+ return quickPulseProcessor ;
136
+ })
137
+ .Build ();
138
+
139
+ var quickPulseModule = new QuickPulseTelemetryModule ();
140
+
141
+ // Secure the control channel.
142
+ // This is optional, but recommended.
143
+ quickPulseModule .AuthenticationApiKey = " YOUR-API-KEY-HERE" ;
144
+ quickPulseModule .Initialize (config );
145
+ quickPulseModule .RegisterTelemetryProcessor (quickPulseProcessor );
146
+
147
+ // Create a TelemetryClient instance. It is important
148
+ // to use the same TelemetryConfiguration here as the one
149
+ // used to set up Live Metrics.
150
+ TelemetryClient client = new TelemetryClient (config );
151
+
152
+ // This sample runs indefinitely. Replace with actual application logic.
153
+ while (true )
154
+ {
155
+ // Send dependency and request telemetry.
156
+ // These will be shown in Live Metrics.
157
+ // CPU/Memory Performance counter is also shown
158
+ // automatically without any additional steps.
159
+ client .TrackDependency (" My dependency" , " target" , " http://sample" ,
160
+ DateTimeOffset .Now , TimeSpan .FromMilliseconds (300 ), true );
161
+ client .TrackRequest (" My Request" , DateTimeOffset .Now ,
162
+ TimeSpan .FromMilliseconds (230 ), " 200" , true );
163
+ Task .Delay (1000 ).Wait ();
114
164
}
115
165
}
116
166
}
117
- ```
167
+ }
168
+ ```
169
+
170
+ ---
118
171
119
172
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
173
@@ -222,6 +275,15 @@ using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPuls
222
275
223
276
Then modify the ` ConfigureServices ` method:
224
277
278
+ # [ .NET 6.0+] ( #tab/dotnet6 )
279
+
280
+ ``` csharp
281
+ // Existing code which includes services.AddApplicationInsightsTelemetry() to enable Application Insights.
282
+ builder .Services .ConfigureTelemetryModule <QuickPulseTelemetryModule > ((module , o ) => module .AuthenticationApiKey = " YOUR-API-KEY-HERE" );
283
+ ```
284
+
285
+ # [ .NET 5.0] ( #tab/dotnet5 )
286
+
225
287
``` csharp
226
288
public void ConfigureServices (IServiceCollection services )
227
289
{
@@ -230,6 +292,8 @@ public void ConfigureServices(IServiceCollection services)
230
292
}
231
293
```
232
294
295
+ ---
296
+
233
297
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
298
235
299
#### WorkerService
0 commit comments