@@ -22,12 +22,52 @@ In this article, you'll learn how to capture logs with Application Insights in .
22
22
23
23
## ASP.NET Core applications
24
24
25
- To add Application Insights logging to ASP.NET Core applications, use the ` Microsoft.Extensions.Logging.ApplicationInsights ` NuGet provider package.
25
+ To add Application Insights logging to ASP.NET Core applications:
26
26
27
- 1 . Install the [ ` Microsoft.Extensions.Logging.ApplicationInsights ` ] [ nuget-ai ] NuGet package .
27
+ 1 . Install the [ ` Microsoft.Extensions.Logging.ApplicationInsights ` ] [ nuget-ai ] .
28
28
29
29
1 . Add ` ApplicationInsightsLoggerProvider ` :
30
30
31
+ # [ .NET 6.0+] ( #tab/dotnet6 )
32
+
33
+ ``` csharp
34
+ using Microsoft .Extensions .Logging .ApplicationInsights ;
35
+
36
+ var builder = WebApplication .CreateBuilder (args );
37
+
38
+ // Add services to the container.
39
+
40
+ builder .Services .AddControllers ();
41
+ // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
42
+ builder .Services .AddEndpointsApiExplorer ();
43
+ builder .Services .AddSwaggerGen ();
44
+
45
+ builder .Logging .AddApplicationInsights (
46
+ configureTelemetryConfiguration : (config ) =>
47
+ config .ConnectionString = builder .Configuration .GetConnectionString (" APPLICATIONINSIGHTS_CONNECTION_STRING" ),
48
+ configureApplicationInsightsLoggerOptions : (options ) => { }
49
+ );
50
+
51
+ builder .Logging .AddFilter <ApplicationInsightsLoggerProvider >(" your-category" , LogLevel .Trace );
52
+
53
+ var app = builder .Build ();
54
+
55
+ // Configure the HTTP request pipeline.
56
+ if (app .Environment .IsDevelopment ())
57
+ {
58
+ app .UseSwagger ();
59
+ app .UseSwaggerUI ();
60
+ }
61
+
62
+ app .UseHttpsRedirection ();
63
+
64
+ app .UseAuthorization ();
65
+
66
+ app .MapControllers ();
67
+
68
+ app .Run ();
69
+ ```
70
+
31
71
# [ .NET 5.0] ( #tab/dotnet5 )
32
72
33
73
``` csharp
@@ -72,46 +112,6 @@ namespace WebApplication
72
112
}
73
113
```
74
114
75
- # [ .NET 6.0+] ( #tab/dotnet6 )
76
-
77
- ``` csharp
78
- using Microsoft .Extensions .Logging .ApplicationInsights ;
79
-
80
- var builder = WebApplication .CreateBuilder (args );
81
-
82
- // Add services to the container.
83
-
84
- builder .Services .AddControllers ();
85
- // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
86
- builder .Services .AddEndpointsApiExplorer ();
87
- builder .Services .AddSwaggerGen ();
88
-
89
- builder .Logging .AddApplicationInsights (
90
- configureTelemetryConfiguration : (config ) =>
91
- config .ConnectionString = builder .Configuration .GetConnectionString (" APPLICATIONINSIGHTS_CONNECTION_STRING" ),
92
- configureApplicationInsightsLoggerOptions : (options ) => { }
93
- );
94
-
95
- builder .Logging .AddFilter <ApplicationInsightsLoggerProvider >(" your-category" , LogLevel .Trace );
96
-
97
- var app = builder .Build ();
98
-
99
- // Configure the HTTP request pipeline.
100
- if (app .Environment .IsDevelopment ())
101
- {
102
- app .UseSwagger ();
103
- app .UseSwaggerUI ();
104
- }
105
-
106
- app .UseHttpsRedirection ();
107
-
108
- app .UseAuthorization ();
109
-
110
- app .MapControllers ();
111
-
112
- app .Run ();
113
- ```
114
-
115
115
---
116
116
117
117
With the NuGet package installed, and the provider being registered with dependency injection, the app is ready to log. With constructor injection, either < xref:Microsoft.Extensions.Logging.ILogger > or the generic-type alternative < xref:Microsoft.Extensions.Logging.ILogger%601 > is required. When these implementations are resolved, ` ApplicationInsightsLoggerProvider ` will provide them. Logged messages or exceptions will be sent to Application Insights.
@@ -143,13 +143,51 @@ For more information, see [Logging in ASP.NET Core](/aspnet/core/fundamentals/lo
143
143
144
144
## Console application
145
145
146
- To add Application Insights logging to console applications, first install the following packages:
146
+ To add Application Insights logging to console applications, first install the following NuGet packages:
147
147
148
- * [ ` Microsoft.Extensions.Logging.ApplicationInsights ` ] [ nuget-ai ] NuGet provider package.
149
- * [ ` Microsoft.Extensions.DependencyInjection ` ] [ nuget-ai ] NuGet provider package.
148
+ * [ ` Microsoft.Extensions.Logging.ApplicationInsights ` ] [ nuget-ai ]
149
+ * [ ` Microsoft.Extensions.DependencyInjection ` ] [ nuget-ai ]
150
150
151
151
The following example uses the Microsoft.Extensions.Logging.ApplicationInsights package and demonstrates the default behavior for a console application. The Microsoft.Extensions.Logging.ApplicationInsights package should be used in a console application or whenever you want a bare minimum implementation of Application Insights without the full feature set such as metrics, distributed tracing, sampling, and telemetry initializers.
152
152
153
+ # [ .NET 6.0+] ( #tab/dotnet6 )
154
+
155
+ ``` csharp
156
+ using Microsoft .ApplicationInsights .Channel ;
157
+ using Microsoft .ApplicationInsights .Extensibility ;
158
+ using Microsoft .Extensions .DependencyInjection ;
159
+ using Microsoft .Extensions .Logging ;
160
+
161
+ using var channel = new InMemoryChannel ();
162
+
163
+ try
164
+ {
165
+ IServiceCollection services = new ServiceCollection ();
166
+ services .Configure <TelemetryConfiguration >(config => config .TelemetryChannel = channel );
167
+ services .AddLogging (builder =>
168
+ {
169
+ // Only Application Insights is registered as a logger provider
170
+ builder .AddApplicationInsights (
171
+ configureTelemetryConfiguration : (config ) => config .ConnectionString = " <YourConnectionString>" ,
172
+ configureApplicationInsightsLoggerOptions : (options ) => { }
173
+ );
174
+ });
175
+
176
+ IServiceProvider serviceProvider = services .BuildServiceProvider ();
177
+ ILogger < Program > logger = serviceProvider .GetRequiredService <ILogger <Program >>();
178
+
179
+ logger .LogInformation (" Logger is working..." );
180
+ }
181
+ finally
182
+ {
183
+ // Explicitly call Flush() followed by Delay, as required in console apps.
184
+ // This ensures that even if the application terminates, telemetry is sent to the back end.
185
+ channel .Flush ();
186
+
187
+ await Task .Delay (TimeSpan .FromMilliseconds (1000 ));
188
+ }
189
+ ```
190
+
153
191
# [ .NET 5.0] ( #tab/dotnet5 )
154
192
155
193
``` csharp
@@ -201,44 +239,6 @@ namespace ConsoleApp
201
239
202
240
```
203
241
204
- # [ .NET 6.0+] ( #tab/dotnet6 )
205
-
206
- ``` csharp
207
- using Microsoft .ApplicationInsights .Channel ;
208
- using Microsoft .ApplicationInsights .Extensibility ;
209
- using Microsoft .Extensions .DependencyInjection ;
210
- using Microsoft .Extensions .Logging ;
211
-
212
- using var channel = new InMemoryChannel ();
213
-
214
- try
215
- {
216
- IServiceCollection services = new ServiceCollection ();
217
- services .Configure <TelemetryConfiguration >(config => config .TelemetryChannel = channel );
218
- services .AddLogging (builder =>
219
- {
220
- // Only Application Insights is registered as a logger provider
221
- builder .AddApplicationInsights (
222
- configureTelemetryConfiguration : (config ) => config .ConnectionString = " <YourConnectionString>" ,
223
- configureApplicationInsightsLoggerOptions : (options ) => { }
224
- );
225
- });
226
-
227
- IServiceProvider serviceProvider = services .BuildServiceProvider ();
228
- ILogger < Program > logger = serviceProvider .GetRequiredService <ILogger <Program >>();
229
-
230
- logger .LogInformation (" Logger is working..." );
231
- }
232
- finally
233
- {
234
- // Explicitly call Flush() followed by Delay, as required in console apps.
235
- // This ensures that even if the application terminates, telemetry is sent to the back end.
236
- channel .Flush ();
237
-
238
- await Task .Delay (TimeSpan .FromMilliseconds (1000 ));
239
- }
240
- ```
241
-
242
242
---
243
243
244
244
## Frequently asked questions
0 commit comments