Skip to content

Commit abe544a

Browse files
authored
Merge pull request #56 from serilog/sp-tr
Include Serilog 3.1 trace and span ids in output
2 parents 2db445d + 8d01ad2 commit abe544a

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

src/Serilog.Formatting.Compact/Formatting/Compact/CompactJsonFormatter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Serilog.Events;
2020
using Serilog.Formatting.Json;
2121
using Serilog.Parsing;
22+
// ReSharper disable MemberCanBePrivate.Global
2223

2324
namespace Serilog.Formatting.Compact
2425
{
@@ -101,6 +102,20 @@ public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFo
101102
JsonValueFormatter.WriteQuotedJsonString(logEvent.Exception.ToString(), output);
102103
}
103104

105+
if (logEvent.TraceId != null)
106+
{
107+
output.Write(",\"@tr\":\"");
108+
output.Write(logEvent.TraceId.Value.ToHexString());
109+
output.Write('\"');
110+
}
111+
112+
if (logEvent.SpanId != null)
113+
{
114+
output.Write(",\"@sp\":\"");
115+
output.Write(logEvent.SpanId.Value.ToHexString());
116+
output.Write('\"');
117+
}
118+
104119
foreach (var property in logEvent.Properties)
105120
{
106121
var name = property.Key;

src/Serilog.Formatting.Compact/Formatting/Compact/RenderedCompactJsonFormatter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.IO;
1818
using Serilog.Events;
1919
using Serilog.Formatting.Json;
20+
// ReSharper disable MemberCanBePrivate.Global
2021

2122
namespace Serilog.Formatting.Compact
2223
{
@@ -83,7 +84,21 @@ public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFo
8384
output.Write(",\"@x\":");
8485
JsonValueFormatter.WriteQuotedJsonString(logEvent.Exception.ToString(), output);
8586
}
87+
88+
if (logEvent.TraceId != null)
89+
{
90+
output.Write(",\"@tr\":\"");
91+
output.Write(logEvent.TraceId.Value.ToHexString());
92+
output.Write('\"');
93+
}
8694

95+
if (logEvent.SpanId != null)
96+
{
97+
output.Write(",\"@sp\":\"");
98+
output.Write(logEvent.SpanId.Value.ToHexString());
99+
output.Write('\"');
100+
}
101+
87102
foreach (var property in logEvent.Properties)
88103
{
89104
var name = property.Key;

src/Serilog.Formatting.Compact/Serilog.Formatting.Compact.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919
<RepositoryUrl>https://github.com/serilog/serilog-formatting-compact</RepositoryUrl>
2020
<RepositoryType>git</RepositoryType>
2121
<AnalysisLevel>6.0-recommended</AnalysisLevel>
22+
<RootNamespace>Serilog</RootNamespace>
23+
<PackageReadmeFile>README.md</PackageReadmeFile>
2224
</PropertyGroup>
2325

2426
<PropertyGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
2527
<WarningLevel>5</WarningLevel>
2628
</PropertyGroup>
2729

2830
<ItemGroup>
29-
<PackageReference Include="Serilog" Version="3.0.1" />
30-
<None Include="serilog-extension-nuget.png" Pack="True" Visible="False" PackagePath="" />
31+
<PackageReference Include="Serilog" Version="3.1.0-*" />
32+
<None Include="../../assets/serilog-extension-nuget.png" Pack="True" Visible="False" PackagePath="" />
33+
<None Include="../../README.md" Pack="True" Visible="False" PackagePath="" />
3134
</ItemGroup>
3235

3336
</Project>

test/Serilog.Formatting.Compact.Tests/CompactJsonFormatterTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using System;
2+
using System.Diagnostics;
3+
using System.Linq;
24
using Newtonsoft.Json.Linq;
5+
using Serilog.Events;
36
using Xunit;
47
using Serilog.Formatting.Compact.Tests.Support;
8+
using Serilog.Parsing;
59

610

711
namespace Serilog.Formatting.Compact.Tests
@@ -77,5 +81,18 @@ public void TimestampIsUtc()
7781
Assert.True(jobject.TryGetValue("@t", out val));
7882
Assert.EndsWith("Z", val.ToObject<string>());
7983
}
84+
85+
[Fact]
86+
public void TraceAndSpanIdsGenerateValidJson()
87+
{
88+
var traceId = ActivityTraceId.CreateRandom();
89+
var spanId = ActivitySpanId.CreateRandom();
90+
var evt = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null,
91+
new MessageTemplate(Enumerable.Empty<MessageTemplateToken>()), Enumerable.Empty<LogEventProperty>(),
92+
traceId, spanId);
93+
var json = AssertValidJson(log => log.Write(evt));
94+
Assert.Equal(traceId.ToHexString(), json["@tr"]);
95+
Assert.Equal(spanId.ToHexString(), json["@sp"]);
96+
}
8097
}
8198
}

0 commit comments

Comments
 (0)