Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support {TraceId} and {SpanId} output template tokens #146

Merged
merged 1 commit into from
Oct 11, 2023
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
2 changes: 1 addition & 1 deletion sample/ConsoleDemo/ConsoleDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net452;net462;net472;net48;net5.0</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion sample/SyncWritesDemo/SyncWritesDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
80 changes: 44 additions & 36 deletions src/Serilog.Sinks.Console/Serilog.Sinks.Console.csproj
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>A Serilog sink that writes log events to the console/terminal.</Description>
<VersionPrefix>5.0.0</VersionPrefix>
<Authors>Serilog Contributors</Authors>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net462;net471</TargetFrameworks>
<TargetFrameworks>$(TargetFrameworks);netstandard2.1;netstandard2.0;net5.0;net6.0;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageTags>serilog;console;terminal</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<PackageProjectUrl>https://github.com/serilog/serilog-sinks-console</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<RepositoryUrl>https://github.com/serilog/serilog-sinks-console</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<RootNamespace>Serilog</RootNamespace>
<LangVersion>latest</LangVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<PropertyGroup>
<Description>A Serilog sink that writes log events to the console/terminal.</Description>
<VersionPrefix>4.2.0</VersionPrefix>
<Authors>Serilog Contributors</Authors>
<TargetFrameworks>net45;netstandard1.3;netstandard2.0;net5.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageTags>serilog;console;terminal</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<PackageProjectUrl>https://github.com/serilog/serilog-sinks-console</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<RepositoryUrl>https://github.com/serilog/serilog-sinks-console</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<TreatSpecificWarningsAsErrors />
<RootNamespace>Serilog</RootNamespace>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' != 'net462' ">
<DefineConstants>$(DefineConstants);RUNTIME_INFORMATION</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' != 'net45' ">
<DefineConstants>$(DefineConstants);RUNTIME_INFORMATION</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
<DefineConstants>$(DefineConstants);FEATURE_SPAN</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
<DefineConstants>$(DefineConstants);FEATURE_SPAN</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<DefineConstants>$(DefineConstants);FEATURE_SPAN</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<DefineConstants>$(DefineConstants);FEATURE_SPAN</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="3.1.0-*" />
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\assets\icon.png" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\assets\icon.png" Pack="true" Visible="false" PackagePath="" />
<None Include="..\..\README.md" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ public override void Render(LogEvent logEvent, TextWriter output)
output.WriteLine();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public OutputTemplateRenderer(ConsoleTheme theme, string outputTemplate, IFormat
{
renderers.Add(new NewLineTokenRenderer(pt.Alignment));
}
else if (pt.PropertyName == OutputProperties.TraceIdPropertyName)
{
renderers.Add(new TraceIdTokenRenderer(theme, pt));
}
else if (pt.PropertyName == OutputProperties.SpanIdPropertyName)
{
renderers.Add(new SpanIdTokenRenderer(theme, pt));
}
else if (pt.PropertyName == OutputProperties.ExceptionPropertyName)
{
renderers.Add(new ExceptionTokenRenderer(theme, pt));
Expand All @@ -62,7 +70,7 @@ public OutputTemplateRenderer(ConsoleTheme theme, string outputTemplate, IFormat
{
renderers.Add(new TimestampTokenRenderer(theme, pt, formatProvider));
}
else if (pt.PropertyName == "Properties")
else if (pt.PropertyName == OutputProperties.PropertiesPropertyName)
{
renderers.Add(new PropertiesTokenRenderer(theme, pt, template, formatProvider));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.IO;
using Serilog.Events;
using Serilog.Parsing;
using Serilog.Sinks.SystemConsole.Rendering;
using Serilog.Sinks.SystemConsole.Themes;

namespace Serilog.Sinks.SystemConsole.Output;

class SpanIdTokenRenderer : OutputTemplateTokenRenderer
{
readonly ConsoleTheme _theme;
readonly Alignment? _alignment;

public SpanIdTokenRenderer(ConsoleTheme theme, PropertyToken spanIdToken)
{
_theme = theme;
_alignment = spanIdToken.Alignment;
}

public override void Render(LogEvent logEvent, TextWriter output)
{
if (logEvent.SpanId is not { } spanId)
return;

var _ = 0;
using (_theme.Apply(output, ConsoleThemeStyle.Text, ref _))
{
if (_alignment is {} alignment)
Padding.Apply(output, spanId.ToString(), alignment);
else
output.Write(spanId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.IO;
using Serilog.Events;
using Serilog.Parsing;
using Serilog.Sinks.SystemConsole.Rendering;
using Serilog.Sinks.SystemConsole.Themes;

namespace Serilog.Sinks.SystemConsole.Output;

class TraceIdTokenRenderer : OutputTemplateTokenRenderer
{
readonly ConsoleTheme _theme;
readonly Alignment? _alignment;

public TraceIdTokenRenderer(ConsoleTheme theme, PropertyToken traceIdToken)
{
_theme = theme;
_alignment = traceIdToken.Alignment;
}

public override void Render(LogEvent logEvent, TextWriter output)
{
if (logEvent.TraceId is not { } traceId)
return;

var _ = 0;
using (_theme.Apply(output, ConsoleThemeStyle.Text, ref _))
{
if (_alignment is {} alignment)
Padding.Apply(output, traceId.ToString(), alignment);
else
output.Write(traceId);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using Serilog.Events;
using Serilog.Parsing;
using Serilog.Sinks.Console.Tests.Support;
using Serilog.Sinks.SystemConsole.Output;
using Serilog.Sinks.SystemConsole.Themes;
Expand Down Expand Up @@ -188,7 +190,7 @@ public SizeFormatter(IFormatProvider innerFormatProvider)
_innerFormatProvider = innerFormatProvider;
}

public object? GetFormat(Type? formatType)
public object GetFormat(Type? formatType)
{
return formatType == typeof(ICustomFormatter) ? this : _innerFormatProvider.GetFormat(formatType) ?? this;
}
Expand Down Expand Up @@ -374,5 +376,29 @@ public void FormatProviderWithDestructuredProperties(string format, bool shouldU
Assert.Contains(expectedFormattedDate, sw.ToString());
Assert.Contains(expectedFormattedNumber, sw.ToString());
}

[Fact]
public void TraceAndSpanAreEmptyWhenAbsent()
{
var formatter = new OutputTemplateRenderer(ConsoleTheme.None, "{TraceId}/{SpanId}", CultureInfo.InvariantCulture);
var evt = DelegatingSink.GetLogEvent(l => l.Information("Hello, world"));
var sw = new StringWriter();
formatter.Format(evt, sw);
Assert.Equal("/", sw.ToString());
}

[Fact]
public void TraceAndSpanAreIncludedWhenPresent()
{
var traceId = ActivityTraceId.CreateRandom();
var spanId = ActivitySpanId.CreateRandom();
var formatter = new OutputTemplateRenderer(ConsoleTheme.None, "{TraceId}/{SpanId}", CultureInfo.InvariantCulture);
var evt = new LogEvent(DateTimeOffset.Now, LogEventLevel.Debug, null,
new MessageTemplate(Enumerable.Empty<MessageTemplateToken>()), Enumerable.Empty<LogEventProperty>(),
traceId, spanId);
var sw = new StringWriter();
formatter.Format(evt, sw);
Assert.Equal($"{traceId}/{spanId}", sw.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net452;net462;net472;net48;net5.0</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down