Skip to content

Commit

Permalink
Add AspNetCore example for log correlation (#1267)
Browse files Browse the repository at this point in the history
* Add AspNetCore example for log correlation

* remove space

* link to non host console
  • Loading branch information
cijothomas authored Sep 15, 2020
1 parent 91ee24f commit 33f6172
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions docs/logs/correlation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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<Startup>();
});
```

`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)
Expand Down

0 comments on commit 33f6172

Please sign in to comment.