From 33f617299b925615294db932c25c736f569b1c16 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Tue, 15 Sep 2020 09:47:02 -0700 Subject: [PATCH] Add AspNetCore example for log correlation (#1267) * Add AspNetCore example for log correlation * remove space * link to non host console --- docs/logs/correlation/README.md | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) 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)