diff --git a/docs/logs/correlation/README.md b/docs/logs/correlation/README.md index 7a837f6fa71..1945de82e55 100644 --- a/docs/logs/correlation/README.md +++ b/docs/logs/correlation/README.md @@ -3,7 +3,9 @@ Starting from `Microsoft.Extensions.Logging` version `5.0`, logs can be correlated with distributed tracing by enriching each log entry with the information from the enclosing `Activity`. This can be achieved by enabling the -`ActivityTrackingOptions`: +`ActivityTrackingOptions`. In a [non-host console +app](https://docs.microsoft.com/aspnet/core/fundamentals/logging#non-host-console-app), +it can be achieved as shown below. ```csharp using var loggerFactory = LoggerFactory.Create(builder => @@ -14,11 +16,28 @@ using var loggerFactory = LoggerFactory.Create(builder => }); ``` +Please refer to the example [here](./Program.cs). + +In an ASP.NET Core app, the above can be achieved by modifying the host +building, as shown below. + +```csharp +public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureLogging(loggingBuilder => + loggingBuilder.Configure(options => + options.ActivityTrackingOptions = + ActivityTrackingOptions.TraceId + | ActivityTrackingOptions.SpanId)) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); +``` + `Microsoft.Extensions.Logging.ActivityTrackingOptions` supports `TraceId`, `SpanId`, `ParentId`, `TraceFlags` and `TraceState`. -Please refer to the example [here](./Program.cs). - ## References * [ILogger](https://docs.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger)