Skip to content

Latest commit

 

History

History
63 lines (46 loc) · 2.24 KB

log-trace-correlation.md

File metadata and controls

63 lines (46 loc) · 2.24 KB

Log to trace correlation

ILogger

Note

Automatic log to trace correlation provided by OpenTelemetry .NET Automatic Instrumentation currently works only for .NET applications using Microsoft.Extensions.Logging. See #2310 and config for more details.

OpenTelemetry .NET SDK automatically correlates logs to trace data. When logs are emitted in the context of an active trace, trace context fields TraceId, SpanId, TraceState are automatically populated.

The following are logs produced by the sample console application:

"logRecords": [
    {
        "timeUnixNano": "1679392614538226700",
        "severityNumber": 9,
        "severityText": "Information",
        "body": {
            "stringValue": "Success! Today is: {Date:MMMM dd, yyyy}"
        },
        "flags": 1,
        "traceId": "21df288eada1ce4ace6c40f39a6d7ce1",
        "spanId": "a80119e5a05fed5a"
    }
]

Further reading:

log4net

See log4net-bridge.

log4net trace context injection

Important

log4net trace context injection is an experimental feature.

The log4net trace context injection is enabled by default. It can be disabled by setting OTEL_DOTNET_AUTO_LOGS_LOG4NET_INSTRUMENTATION_ENABLED to false.

Context injection is supported for log4net in versions >= 2.0.13 && < 4.0.0

Following properties are set by default on the collection of logging event's properties:

  • trace_id
  • span_id
  • trace_flags

This allows for trace context to be logged into currently configured log destination, e.g. a file. In order to use them, pattern needs to be updated.