File tree 4 files changed +25
-3
lines changed
test/Serilog.AspNetCore.Tests
4 files changed +25
-3
lines changed Original file line number Diff line number Diff line change 1
1
using Serilog ;
2
+ using Serilog . Templates ;
2
3
3
4
namespace Sample ;
4
5
@@ -39,6 +40,8 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
39
40
. ReadFrom . Configuration ( context . Configuration )
40
41
. ReadFrom . Services ( services )
41
42
. 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}" ) ) )
43
46
. ConfigureWebHostDefaults ( webBuilder => webBuilder . UseStartup < Startup > ( ) ) ;
44
- }
47
+ }
Original file line number Diff line number Diff line change 7
7
<ItemGroup >
8
8
<ProjectReference Include =" ..\..\src\Serilog.AspNetCore\Serilog.AspNetCore.csproj" />
9
9
</ItemGroup >
10
+
11
+ <ItemGroup >
12
+ <PackageReference Include =" Serilog.Expressions" Version =" 4.0.0-*" />
13
+ </ItemGroup >
10
14
11
15
</Project >
Original file line number Diff line number Diff line change 27
27
<PackageReference Include =" Serilog.Sinks.Console" Version =" 4.0.1" />
28
28
<PackageReference Include =" Serilog.Sinks.File" Version =" 5.0.0" />
29
29
<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-* " />
31
31
</ItemGroup >
32
32
33
33
<ItemGroup >
36
36
<PackageReference Include =" Serilog.Settings.Configuration" Version =" 7.0.0" />
37
37
<PackageReference Include =" Microsoft.Extensions.DependencyInjection" Version =" 7.0.0" />
38
38
<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-*" />
39
41
</ItemGroup >
40
42
41
43
<ItemGroup Condition =" '$(TargetFramework)' != 'net462' and '$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netstandard2.1' " >
Original file line number Diff line number Diff line change @@ -164,6 +164,19 @@ WebApplicationFactory<TestStartup> Setup(
164
164
165
165
return web ;
166
166
}
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
+ }
167
180
168
181
( SerilogSink , WebApplicationFactory < TestStartup > ) Setup (
169
182
Action < RequestLoggingOptions > ? configureOptions = null ,
You can’t perform that action at this time.
0 commit comments