Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Serilog.Events;
using Serilog.Formatting.Json;
using Serilog.Parsing;
// ReSharper disable MemberCanBePrivate.Global

namespace Serilog.Formatting.Compact
{
Expand Down Expand Up @@ -101,6 +102,20 @@ public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFo
JsonValueFormatter.WriteQuotedJsonString(logEvent.Exception.ToString(), output);
}

if (logEvent.TraceId != null)
{
output.Write(",\"@tr\":\"");
output.Write(logEvent.TraceId.Value.ToHexString());
output.Write('\"');
}

if (logEvent.SpanId != null)
{
output.Write(",\"@sp\":\"");
output.Write(logEvent.SpanId.Value.ToHexString());
output.Write('\"');
}

foreach (var property in logEvent.Properties)
{
var name = property.Key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.IO;
using Serilog.Events;
using Serilog.Formatting.Json;
// ReSharper disable MemberCanBePrivate.Global

namespace Serilog.Formatting.Compact
{
Expand Down Expand Up @@ -83,7 +84,21 @@ public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFo
output.Write(",\"@x\":");
JsonValueFormatter.WriteQuotedJsonString(logEvent.Exception.ToString(), output);
}

if (logEvent.TraceId != null)
{
output.Write(",\"@tr\":\"");
output.Write(logEvent.TraceId.Value.ToHexString());
output.Write('\"');
}

if (logEvent.SpanId != null)
{
output.Write(",\"@sp\":\"");
output.Write(logEvent.SpanId.Value.ToHexString());
output.Write('\"');
}

foreach (var property in logEvent.Properties)
{
var name = property.Key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
<RepositoryUrl>https://github.com/serilog/serilog-formatting-compact</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<AnalysisLevel>6.0-recommended</AnalysisLevel>
<RootNamespace>Serilog</RootNamespace>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

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

<ItemGroup>
<PackageReference Include="Serilog" Version="3.0.1" />
<None Include="serilog-extension-nuget.png" Pack="True" Visible="False" PackagePath="" />
<PackageReference Include="Serilog" Version="3.1.0-*" />
<None Include="../../assets/serilog-extension-nuget.png" Pack="True" Visible="False" PackagePath="" />
<None Include="../../README.md" Pack="True" Visible="False" PackagePath="" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions test/Serilog.Formatting.Compact.Tests/CompactJsonFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.Diagnostics;
using System.Linq;
using Newtonsoft.Json.Linq;
using Serilog.Events;
using Xunit;
using Serilog.Formatting.Compact.Tests.Support;
using Serilog.Parsing;


namespace Serilog.Formatting.Compact.Tests
Expand Down Expand Up @@ -77,5 +81,18 @@ public void TimestampIsUtc()
Assert.True(jobject.TryGetValue("@t", out val));
Assert.EndsWith("Z", val.ToObject<string>());
}

[Fact]
public void TraceAndSpanIdsGenerateValidJson()
{
var traceId = ActivityTraceId.CreateRandom();
var spanId = ActivitySpanId.CreateRandom();
var evt = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null,
new MessageTemplate(Enumerable.Empty<MessageTemplateToken>()), Enumerable.Empty<LogEventProperty>(),
traceId, spanId);
var json = AssertValidJson(log => log.Write(evt));
Assert.Equal(traceId.ToHexString(), json["@tr"]);
Assert.Equal(spanId.ToHexString(), json["@sp"]);
}
}
}