Skip to content

Commit 75c35ba

Browse files
committed
Add test and update sample
1 parent f1c1bd7 commit 75c35ba

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

samples/Sample/Program.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Serilog;
2+
using Serilog.Templates;
23

34
namespace Sample;
45

@@ -39,6 +40,8 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
3940
.ReadFrom.Configuration(context.Configuration)
4041
.ReadFrom.Services(services)
4142
.Enrich.FromLogContext()
42-
.WriteTo.Console())
43+
.WriteTo.Console(new ExpressionTemplate(
44+
// Include trace and span ids when present.
45+
"[{@t:HH:mm:ss} {@l:u3}{#if @tr is not null} ({@tr}:{@sp}){#end}] {@m}\n{@x}")))
4346
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
44-
}
47+
}

samples/Sample/Sample.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
<ItemGroup>
88
<ProjectReference Include="..\..\src\Serilog.AspNetCore\Serilog.AspNetCore.csproj" />
99
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Serilog.Expressions" Version="4.0.0-*" />
13+
</ItemGroup>
1014

1115
</Project>

src/Serilog.AspNetCore/Serilog.AspNetCore.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
2828
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
2929
<PackageReference Include="Serilog.Sinks.Debug" Version="2.0.0" />
30-
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
30+
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0-*" />
3131
</ItemGroup>
3232

3333
<ItemGroup>
@@ -36,6 +36,8 @@
3636
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
3737
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
3838
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
39+
<!-- Temporary addition to pull in trace/span support -->
40+
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.1-*" />
3941
</ItemGroup>
4042

4143
<ItemGroup Condition=" '$(TargetFramework)' != 'net462' and '$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netstandard2.1' ">

test/Serilog.AspNetCore.Tests/SerilogWebHostBuilderExtensionsTests.cs

+13
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ WebApplicationFactory<TestStartup> Setup(
164164

165165
return web;
166166
}
167+
168+
[Fact]
169+
public async Task RequestLoggingMiddlewareShouldAddTraceAndSpanIds()
170+
{
171+
var (sink, web) = Setup();
172+
173+
await web.CreateClient().GetAsync("/resource");
174+
175+
var completionEvent = sink.Writes.First(logEvent => Matching.FromSource<RequestLoggingMiddleware>()(logEvent));
176+
177+
Assert.NotNull(completionEvent.TraceId);
178+
Assert.NotNull(completionEvent.SpanId);
179+
}
167180

168181
(SerilogSink, WebApplicationFactory<TestStartup>) Setup(
169182
Action<RequestLoggingOptions>? configureOptions = null,

0 commit comments

Comments
 (0)