Skip to content

Commit

Permalink
propagates current trace and span ids (manigandham#67)
Browse files Browse the repository at this point in the history
Serilog starting from v3.1.0 adds two new first-class properties to LogEvent: TraceId and SpanId. These are set automatically in Logger.Write() to the corresponding property values from System.Diagnostics.Activity.Current.
  • Loading branch information
Stanislav Perekrestov committed Jun 3, 2024
1 parent b5735b8 commit a6608ab
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ private LogEntry CreateLogEntry(LogEvent evnt, StringWriter writer)
HandleSpecialProperty(log, property.Key, property.Value);
}

if (_sinkOptions.UseLogCorrelation)
{
if (evnt.TraceId.ToString() is { Length: > 0 } traceId)
{
log.Trace = $"projects/{_projectId}/traces/{traceId}";
}
if (evnt.SpanId?.ToString() is { Length: > 0 } spanId)
{
log.SpanId = spanId;
}
}

if (_serviceContext != null)
jsonPayload.Fields.Add("serviceContext", Value.ForStruct(_serviceContext));

Expand All @@ -119,12 +131,6 @@ private void HandleSpecialProperty(LogEntry log, string key, LogEventPropertyVal

if (_sinkOptions.UseLogCorrelation)
{
if (key.Equals("TraceId", StringComparison.OrdinalIgnoreCase))
log.Trace = $"projects/{_projectId}/traces/{GetString(value)}";

if (key.Equals("SpanId", StringComparison.OrdinalIgnoreCase))
log.SpanId = GetString(value);

if (key.Equals("TraceSampled", StringComparison.OrdinalIgnoreCase))
log.TraceSampled = GetBoolean(value);
}
Expand Down

0 comments on commit a6608ab

Please sign in to comment.