55using System . Threading . Tasks ;
66using FluentAssertions ;
77using Serilog . Core ;
8+ using Serilog . Core . Enrichers ;
89using Serilog . Events ;
910using Serilog . Formatting ;
1011using Serilog . Sinks . Network . Formatters ;
@@ -78,7 +79,7 @@ public async Task IncludesCurrentActivityTraceAndSpanIds()
7879
7980 var receivedData = await ServerPoller . PollForReceivedData ( fixture . Socket ) ;
8081
81- receivedData . Should ( ) . Contain ( $ "\" traceId\" :\" { activity ! . TraceId } \" ") ;
82+ receivedData . Should ( ) . Contain ( $ "\" traceId\" :\" { activity . TraceId } \" ") ;
8283 receivedData . Should ( ) . Contain ( $ "\" spanId\" :\" { activity . SpanId } \" ") ;
8384 }
8485
@@ -95,42 +96,36 @@ public async Task OmitsTraceAndSpanIdsWhenThereIsNoActivity()
9596 receivedData . Should ( ) . NotContain ( "\" spanId\" " ) ;
9697 }
9798
99+ // The following test documents and validates the current behavior, but this could change
100+ // depending on how https://github.com/serilog-contrib/Serilog.Sinks.Network/issues/39 is
101+ // resolved.
98102 [ Fact ]
99- public async Task DoesNotAddDuplicateTraceAndSpanIds ( )
103+ public async Task WritesTraceAndSpanIdsBeforeDuplicatePropertiesFromEnrichers ( )
100104 {
101105 using var activitySource = new ActivitySource ( "TestSource" ) ;
102106 using var activityListener = CreateAndAddActivityListener ( activitySource . Name ) ;
103107 using var activity = activitySource . StartActivity ( ) ;
104108 Assert . NotNull ( activity ) ;
105109
106110 using var fixture = ConfigureTestLogger (
107- new LogstashJsonFormatter ( ) ,
108- // This enricher will add traceId and spanId properties to the log event:
109- [ new TraceAndSpanEnricher ( ) ]
111+ new LogstashJsonFormatter ( ) ,
112+ [
113+ new PropertyEnricher ( "traceId" , "traceId-from-enricher" ) ,
114+ new PropertyEnricher ( "spanId" , "spanId-from-enricher" )
115+ ]
110116 ) ;
111117
112118 fixture . Logger . Information ( "arbitraryMessage" ) ;
113119
114120 var receivedData = await ServerPoller . PollForReceivedData ( fixture . Socket ) ;
115121
116- // Count the occurrences of traceId and spanId in the received data:
117- var traceIdCount = receivedData . Split ( "\" traceId\" " ) . Length - 1 ;
118- traceIdCount . Should ( ) . Be ( 1 , "traceId should only appear once in the log message." ) ;
119- var spanIdCount = receivedData . Split ( "\" spanId\" " ) . Length - 1 ;
120- spanIdCount . Should ( ) . Be ( 1 , "spanId should only appear once in the log message." ) ;
121- }
122+ var indexOfTraceId = receivedData . IndexOf ( $ "\" traceId\" :\" { activity . TraceId } \" ") ;
123+ var indexOfTraceIdFromEnricher = receivedData . IndexOf ( "\" traceId\" :\" traceId-from-enricher\" " ) ;
124+ indexOfTraceId . Should ( ) . BePositive ( ) . And . BeLessThan ( indexOfTraceIdFromEnricher ) ;
122125
123- private class TraceAndSpanEnricher : ILogEventEnricher
124- {
125- public void Enrich ( LogEvent logEvent , ILogEventPropertyFactory propertyFactory )
126- {
127- var currentActivity = Activity . Current ;
128- if ( currentActivity != null )
129- {
130- logEvent . AddOrUpdateProperty ( propertyFactory . CreateProperty ( "traceId" , currentActivity . TraceId . ToString ( ) ) ) ;
131- logEvent . AddOrUpdateProperty ( propertyFactory . CreateProperty ( "spanId" , currentActivity . SpanId . ToString ( ) ) ) ;
132- }
133- }
126+ var indexOfSpanId = receivedData . IndexOf ( $ "\" spanId\" :\" { activity . SpanId } \" ") ;
127+ var indexOfSpanIdFromEnricher = receivedData . IndexOf ( "\" spanId\" :\" spanId-from-enricher\" " ) ;
128+ indexOfSpanId . Should ( ) . BePositive ( ) . And . BeLessThan ( indexOfSpanIdFromEnricher ) ;
134129 }
135130
136131 private static ActivityListener CreateAndAddActivityListener ( string sourceName )
0 commit comments