Skip to content

Commit 77ac5aa

Browse files
authored
Merge pull request #102 from nblumhardt/sp-tr
Add support for Serilog 3.1 trace `@tr` and span `@sp` ids
2 parents 938b02d + 2f35b97 commit 77ac5aa

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

src/Serilog.Expressions/Expressions/BuiltInProperty.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ static class BuiltInProperty
2525
public const string Properties = "p";
2626
public const string Renderings = "r";
2727
public const string EventId = "i";
28+
public const string TraceId = "tr";
29+
public const string SpanId = "sp";
2830
}

src/Serilog.Expressions/Expressions/Compilation/Linq/LinqExpressionCompiler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ protected override ExpressionBody Transform(AmbientNameExpression px)
206206
BuiltInProperty.Message => Splice(context => new ScalarValue(Intrinsics.RenderMessage(formatter, context))),
207207
BuiltInProperty.Exception => Splice(context =>
208208
context.LogEvent.Exception == null ? null : new ScalarValue(context.LogEvent.Exception)),
209+
BuiltInProperty.TraceId => Splice(context =>
210+
context.LogEvent.TraceId == null ? null : new ScalarValue(context.LogEvent.TraceId.Value)),
211+
BuiltInProperty.SpanId => Splice(context =>
212+
context.LogEvent.SpanId == null ? null : new ScalarValue(context.LogEvent.SpanId.Value)),
209213
BuiltInProperty.Timestamp => Splice(context => new ScalarValue(context.LogEvent.Timestamp)),
210214
BuiltInProperty.MessageTemplate => Splice(context => new ScalarValue(context.LogEvent.MessageTemplate.Text)),
211215
BuiltInProperty.Properties => Splice(context =>

src/Serilog.Expressions/Expressions/Runtime/Coerce.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System.Diagnostics;
1516
using System.Diagnostics.CodeAnalysis;
1617
using Serilog.Events;
1718

@@ -77,6 +78,18 @@ public static bool String(LogEventPropertyValue? value, [MaybeNullWhen(false)] o
7778
str = sv.Value.ToString()!;
7879
return true;
7980
}
81+
82+
if (sv.Value is ActivityTraceId traceId)
83+
{
84+
str = traceId.ToHexString();
85+
return true;
86+
}
87+
88+
if (sv.Value is ActivitySpanId spanId)
89+
{
90+
str = spanId.ToHexString();
91+
return true;
92+
}
8093
}
8194

8295
str = default;

src/Serilog.Expressions/Serilog.Expressions.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
1515
<RepositoryUrl>https://github.com/serilog/serilog-expressions</RepositoryUrl>
1616
<RepositoryType>git</RepositoryType>
17+
<PackageReadmeFile>README.md</PackageReadmeFile>
1718
</PropertyGroup>
1819

1920
<ItemGroup>
20-
<PackageReference Include="Serilog" Version="3.0.1" />
21+
<PackageReference Include="Serilog" Version="3.1.0-*" />
2122
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="All" />
2223
</ItemGroup>
2324

2425
<ItemGroup>
2526
<None Include="..\..\assets\icon.png" Pack="true" Visible="false" PackagePath="" />
27+
<None Include="..\..\README.md" Pack="true" Visible="false" PackagePath="" />
2628
</ItemGroup>
2729

2830
</Project>

test/Serilog.Expressions.Tests/Cases/expression-evaluation-cases.asv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,12 @@ undefined() = undefined() ci ⇶ undefined()
304304
'test' like '%' ⇶ true
305305
'test' like '' ⇶ false
306306
'' like '' ⇶ true
307+
308+
// Built-ins
309+
310+
@m ⇶ 'Hello, World!'
311+
@mt ⇶ 'Hello, {Name}!'
312+
tostring(@x) like 'System.DivideByZeroException%' ⇶ true
313+
@l ⇶ 'Warning'
314+
@sp ⇶ 'bb1111820570b80e'
315+
@tr ⇶ '1befc31e94b01d1a473f63a7905f6c9b'

test/Serilog.Expressions.Tests/ExpressionEvaluationTests.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System.Diagnostics;
12
using System.Globalization;
23
using Serilog.Events;
34
using Serilog.Expressions.Runtime;
45
using Serilog.Expressions.Tests.Support;
6+
using Serilog.Parsing;
57
using Xunit;
68

79
namespace Serilog.Expressions.Tests;
@@ -15,14 +17,22 @@ public class ExpressionEvaluationTests
1517
[MemberData(nameof(ExpressionEvaluationCases))]
1618
public void ExpressionsAreCorrectlyEvaluated(string expr, string result)
1719
{
18-
var evt = Some.InformationEvent();
19-
20-
evt.AddPropertyIfAbsent(
21-
new("User", new StructureValue(new[]
20+
var evt = new LogEvent(
21+
new DateTimeOffset(2025, 5, 15, 13, 12, 11, 789, TimeSpan.FromHours(10)),
22+
LogEventLevel.Warning,
23+
new DivideByZeroException(),
24+
new MessageTemplateParser().Parse("Hello, {Name}!"),
25+
new []
2226
{
23-
new LogEventProperty("Id", new ScalarValue(42)),
24-
new LogEventProperty("Name", new ScalarValue("nblumhardt")),
25-
})));
27+
new LogEventProperty("Name", new ScalarValue("World")),
28+
new LogEventProperty("User", new StructureValue(new[]
29+
{
30+
new LogEventProperty("Id", new ScalarValue(42)),
31+
new LogEventProperty("Name", new ScalarValue("nblumhardt")),
32+
}))
33+
},
34+
ActivityTraceId.CreateFromString("1befc31e94b01d1a473f63a7905f6c9b"),
35+
ActivitySpanId.CreateFromString("bb1111820570b80e"));
2636

2737
var frFr = CultureInfo.GetCultureInfoByIetfLanguageTag("fr-FR");
2838
var testHelpers = new TestHelperNameResolver();

0 commit comments

Comments
 (0)