Skip to content

Commit 8317a14

Browse files
authored
Merge pull request #202457 from AaronMaxwell/aaronmax-ilogger-string-update
Aaronmax ilogger string update
2 parents 6a2bb2e + c202a3d commit 8317a14

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

articles/azure-monitor/app/ilogger.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ In this article, you'll learn how to capture logs with Application Insights in .
2323
[nuget-ai-ws-tc]: https://www.nuget.org/packages/Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel
2424

2525
> [!TIP]
26-
> The [`Microsoft.ApplicationInsights.WorkerService`][nuget-ai-ws] NuGet package is beyond the scope of this article. It can be used to enable Application Insights for background services. For more information, see [Application Insights for Worker Service apps](./worker-service.md).
26+
> The [`Microsoft.ApplicationInsights.WorkerService`][nuget-ai-ws] NuGet package, used to enable Application Insights for background services, is out of scope. For more information, see [Application Insights for Worker Service apps](./worker-service.md).
2727
2828
Depending on the Application Insights logging package that you use, there will be various ways to register `ApplicationInsightsLoggerProvider`. `ApplicationInsightsLoggerProvider` is an implementation of <xref:Microsoft.Extensions.Logging.ILoggerProvider>, which is responsible for providing <xref:Microsoft.Extensions.Logging.ILogger> and <xref:Microsoft.Extensions.Logging.ILogger%601> implementations.
2929

3030
## ASP.NET Core applications
3131

32-
To add Application Insights telemetry to ASP.NET Core applications, use the `Microsoft.ApplicationInsights.AspNetCore` NuGet package. You can configure this through [Visual Studio as a connected service](/visualstudio/azure/azure-app-insights-add-connected-service), or manually.
32+
To add Application Insights telemetry to ASP.NET Core applications, use the `Microsoft.ApplicationInsights.AspNetCore` NuGet package. You can configure this telemetry through [Visual Studio as a connected service](/visualstudio/azure/azure-app-insights-add-connected-service), or manually.
3333

3434
By default, ASP.NET Core applications have an Application Insights logging provider registered when they're configured through the [code](./asp-net-core.md) or [codeless](./azure-web-apps-net-core.md#enable-auto-instrumentation-monitoring) approach. The registered provider is configured to automatically capture log events with a severity of <xref:Microsoft.Extensions.Logging.LogLevel.Warning?displayProperty=nameWithType> or greater. You can customize severity and categories. For more information, see [Logging level](#logging-level).
3535

@@ -65,7 +65,7 @@ By default, ASP.NET Core applications have an Application Insights logging provi
6565
public void ConfigureServices(IServiceCollection services)
6666
{
6767
services.AddApplicationInsightsTelemetry();
68-
// Configure the Connection String/Instrumentation key in appsettings.json
68+
// Configure the Connection String in appsettings.json
6969
}
7070

7171
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
@@ -111,9 +111,9 @@ There are several limitations when you're logging from *Program.cs* and *Startup
111111

112112
* Telemetry is sent through the [InMemoryChannel](./telemetry-channels.md) telemetry channel.
113113
* No [sampling](./sampling.md) is applied to telemetry.
114-
* Standard [telemetry initializers or processors](./api-filtering-sampling.md) are not available.
114+
* Standard [telemetry initializers or processors](./api-filtering-sampling.md) aren't available.
115115

116-
The following examples demonstrate this by explicitly instantiating and configuring *Program.cs* and *Startup.cs*.
116+
The following examples provide a demonstration by explicitly instantiating and configuring *Program.cs* and *Startup.cs*.
117117

118118
#### Example Program.cs
119119

@@ -146,12 +146,14 @@ namespace WebApplication
146146
})
147147
.ConfigureLogging((context, builder) =>
148148
{
149-
// Providing an instrumentation key is required if you're using the
149+
// Providing a connection string is required if you're using the
150150
// standalone Microsoft.Extensions.Logging.ApplicationInsights package,
151151
// or when you need to capture logs during application startup, such as
152152
// in Program.cs or Startup.cs itself.
153153
builder.AddApplicationInsights(
154-
context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"]);
154+
configureTelemetryConfiguration: (config) => config.ConnectionString = context.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"],
155+
configureApplicationInsightsLoggerOptions: (options) => { }
156+
);
155157

156158
// Capture all log-level entries from Program
157159
builder.AddFilter<ApplicationInsightsLoggerProvider>(
@@ -165,9 +167,6 @@ namespace WebApplication
165167
}
166168
```
167169

168-
In the preceding code, `ApplicationInsightsLoggerProvider` is configured with your `"APPINSIGHTS_INSTRUMENTATIONKEY"` instrumentation key. Filters are applied, setting the log level to <xref:Microsoft.Extensions.Logging.LogLevel.Trace?displayProperty=nameWithType>.
169-
170-
171170
[!INCLUDE [azure-monitor-log-analytics-rebrand](../../../includes/azure-monitor-instrumentation-key-deprecation.md)]
172171

173172
#### Example Startup.cs
@@ -195,7 +194,7 @@ namespace WebApplication
195194
public void ConfigureServices(IServiceCollection services)
196195
{
197196
services.AddApplicationInsightsTelemetry();
198-
// Configure the Connection String/Instrumentation key in appsettings.json
197+
// Configure the Connection String in appsettings.json
199198
}
200199

201200
// The ILogger<Startup> is resolved by dependency injection
@@ -260,7 +259,10 @@ namespace ConsoleApp
260259
services.AddLogging(builder =>
261260
{
262261
// Only Application Insights is registered as a logger provider
263-
builder.AddApplicationInsights("<YourInstrumentationKey>");
262+
builder.AddApplicationInsights(
263+
configureTelemetryConfiguration: (config) => config.ConnectionString = "<YourConnectionString>",
264+
configureApplicationInsightsLoggerOptions: (options) => { }
265+
);
264266
});
265267

266268
IServiceProvider serviceProvider = services.BuildServiceProvider();
@@ -284,7 +286,7 @@ namespace ConsoleApp
284286

285287
The preceding example uses the `Microsoft.Extensions.Logging.ApplicationInsights` package. By default, this configuration uses the "bare minimum" `TelemetryConfiguration` setup for sending data to Application Insights: the `InMemoryChannel` channel. There's no sampling and no standard `TelemetryInitializer` instance. You can override this behavior for a console application, as the following example shows.
286288

287-
Install this additional package:
289+
Also install this package:
288290

289291
```xml
290292
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.17.0" />
@@ -327,7 +329,10 @@ namespace ConsoleApp
327329
services.AddLogging(builder =>
328330
{
329331
// Only Application Insights is registered as a logger provider
330-
builder.AddApplicationInsights("<YourInstrumentationKey>");
332+
builder.AddApplicationInsights(
333+
configureTelemetryConfiguration: (config) => config.ConnectionString = "<YourConnectionString>",
334+
configureApplicationInsightsLoggerOptions: (options) => { }
335+
);
331336
});
332337

333338
IServiceProvider serviceProvider = services.BuildServiceProvider();
@@ -356,7 +361,7 @@ The following examples show how to apply filter rules to `ApplicationInsightsLog
356361

357362
### Create filter rules in configuration with appsettings.json
358363

359-
`ApplicationInsightsLoggerProvider` is aliased as "ApplicationInsights." The following section of *appsettings.json* overrides the default <xref:Microsoft.Extensions.Logging.LogLevel.Warning?displayProperty=nameWithType> log level of Application Insights to log categories that start with "Microsoft" at level <xref:Microsoft.Extensions.Logging.LogLevel.Error?displayProperty=nameWithType> and higher.
364+
`ApplicationInsightsLoggerProvider` is aliased as "ApplicationInsights". The following section of *appsettings.json* overrides the default <xref:Microsoft.Extensions.Logging.LogLevel.Warning?displayProperty=nameWithType> log level of Application Insights to log categories that start with "Microsoft" at level <xref:Microsoft.Extensions.Logging.LogLevel.Error?displayProperty=nameWithType> and higher.
360365

361366
```json
362367
{
@@ -475,11 +480,11 @@ Here's the change in the *appsettings.json* file:
475480

476481
### Why do some ILogger logs not have the same properties as others?
477482

478-
Application Insights captures and sends `ILogger` logs by using the same `TelemetryConfiguration` information that's used for every other telemetry. But there's an exception. By default, `TelemetryConfiguration` is not fully set up when you log from *Program.cs* or *Startup.cs*. Logs from these places won't have the default configuration, so they won't be running all `TelemetryInitializer` instances and `TelemetryProcessor` instances.
483+
Application Insights captures and sends `ILogger` logs by using the same `TelemetryConfiguration` information that's used for every other telemetry. But there's an exception. By default, `TelemetryConfiguration` isn't fully set up when you log from *Program.cs* or *Startup.cs*. Logs from these places won't have the default configuration, so they won't be running all `TelemetryInitializer` instances and `TelemetryProcessor` instances.
479484

480-
### I'm using the standalone package Microsoft.Extensions.Logging.ApplicationInsights, and I want to log some additional custom telemetry manually. How should I do that?
485+
### I'm using the standalone package Microsoft.Extensions.Logging.ApplicationInsights, and I want to log more custom telemetry manually. How should I do that?
481486

482-
When you use the standalone package, `TelemetryClient` is not injected to the dependency injection (DI) container. You need to create a new instance of `TelemetryClient` and use the same configuration that the logger provider uses, as the following code shows. This ensures that the same configuration is used for all custom telemetry and telemetry from `ILogger`.
487+
When you use the standalone package, `TelemetryClient` isn't injected to the dependency injection (DI) container. You need to create a new instance of `TelemetryClient` and use the same configuration that the logger provider uses, as the following code shows. This requirement ensures that the same configuration is used for all custom telemetry and telemetry from `ILogger`.
483488

484489
```csharp
485490
public class MyController : ApiController
@@ -518,7 +523,7 @@ The Application Insights extension in Azure Web Apps uses the new provider. You
518523

519524
### I can't see some of the logs from my application in the workspace.
520525

521-
This may happen because of adaptive sampling. Adaptive sampling is enabled by default in all the latest versions of the Application Insights ASP.NET and ASP.NET Core Software Development Kits (SDKs). See the [Sampling in Application Insights](./sampling.md) for more details.
526+
Missing data can occur due to adaptive sampling. Adaptive sampling is enabled by default in all the latest versions of the Application Insights ASP.NET and ASP.NET Core Software Development Kits (SDKs). See the [Sampling in Application Insights](./sampling.md) for more details.
522527

523528
## Next steps
524529

0 commit comments

Comments
 (0)