@@ -42,7 +42,7 @@ public void CreateContextWithDisabledLoggerDoesNotCreateActivity()
4242 }
4343
4444 [ Fact ]
45- public void CreateContextWithEnabledLoggerCreatesActivityAndSetsActivityIdInScope ( )
45+ public void CreateContextWithEnabledLoggerCreatesActivityAndSetsActivityInScope ( )
4646 {
4747 // Arrange
4848 var logger = new LoggerWithScopes ( isEnabled : true ) ;
@@ -53,7 +53,36 @@ public void CreateContextWithEnabledLoggerCreatesActivityAndSetsActivityIdInScop
5353
5454 Assert . Single ( logger . Scopes ) ;
5555 var pairs = ( ( IReadOnlyList < KeyValuePair < string , object > > ) logger . Scopes [ 0 ] ) . ToDictionary ( p => p . Key , p => p . Value ) ;
56- Assert . Equal ( Activity . Current . Id , pairs [ "ActivityId" ] . ToString ( ) ) ;
56+ Assert . Equal ( Activity . Current . Id , pairs [ "SpanId" ] . ToString ( ) ) ;
57+ Assert . Equal ( Activity . Current . RootId , pairs [ "TraceId" ] . ToString ( ) ) ;
58+ Assert . Equal ( string . Empty , pairs [ "ParentId" ] ? . ToString ( ) ) ;
59+ }
60+
61+ [ Fact ]
62+ public void CreateContextWithEnabledLoggerAndRequestIdCreatesActivityAndSetsActivityInScope ( )
63+ {
64+ // Arrange
65+
66+ // Generate an id we can use for the request id header (in the correct format)
67+ var activity = new Activity ( "IncomingRequest" ) ;
68+ activity . Start ( ) ;
69+ var id = activity . Id ;
70+ activity . Stop ( ) ;
71+
72+ var logger = new LoggerWithScopes ( isEnabled : true ) ;
73+ var hostingApplication = CreateApplication ( out var features , logger : logger , configure : context =>
74+ {
75+ context . Request . Headers [ "Request-Id" ] = id ;
76+ } ) ;
77+
78+ // Act
79+ var context = hostingApplication . CreateContext ( features ) ;
80+
81+ Assert . Single ( logger . Scopes ) ;
82+ var pairs = ( ( IReadOnlyList < KeyValuePair < string , object > > ) logger . Scopes [ 0 ] ) . ToDictionary ( p => p . Key , p => p . Value ) ;
83+ Assert . Equal ( Activity . Current . Id , pairs [ "SpanId" ] . ToString ( ) ) ;
84+ Assert . Equal ( Activity . Current . RootId , pairs [ "TraceId" ] . ToString ( ) ) ;
85+ Assert . Equal ( id , pairs [ "ParentId" ] . ToString ( ) ) ;
5786 }
5887
5988 [ Fact ]
@@ -90,10 +119,6 @@ public void ActivityStopDoesNotFireIfNoListenerAttachedForStart()
90119 // Act
91120 var context = hostingApplication . CreateContext ( features ) ;
92121
93- Assert . Single ( logger . Scopes ) ;
94- var pairs = ( ( IReadOnlyList < KeyValuePair < string , object > > ) logger . Scopes [ 0 ] ) . ToDictionary ( p => p . Key , p => p . Value ) ;
95- Assert . Equal ( Activity . Current . Id , pairs [ "ActivityId" ] . ToString ( ) ) ;
96-
97122 hostingApplication . DisposeContext ( context , exception : null ) ;
98123
99124 Assert . False ( startFired ) ;
@@ -398,13 +423,15 @@ private static void AssertProperty<T>(object o, string name)
398423 }
399424
400425 private static HostingApplication CreateApplication ( out FeatureCollection features ,
401- DiagnosticListener diagnosticSource = null , ILogger logger = null )
426+ DiagnosticListener diagnosticSource = null , ILogger logger = null , Action < DefaultHttpContext > configure = null )
402427 {
403428 var httpContextFactory = new Mock < IHttpContextFactory > ( ) ;
404429
405430 features = new FeatureCollection ( ) ;
406431 features . Set < IHttpRequestFeature > ( new HttpRequestFeature ( ) ) ;
407- httpContextFactory . Setup ( s => s . Create ( It . IsAny < IFeatureCollection > ( ) ) ) . Returns ( new DefaultHttpContext ( features ) ) ;
432+ var context = new DefaultHttpContext ( features ) ;
433+ configure ? . Invoke ( context ) ;
434+ httpContextFactory . Setup ( s => s . Create ( It . IsAny < IFeatureCollection > ( ) ) ) . Returns ( context ) ;
408435 httpContextFactory . Setup ( s => s . Dispose ( It . IsAny < HttpContext > ( ) ) ) ;
409436
410437 var hostingApplication = new HostingApplication (
@@ -453,7 +480,7 @@ public IDisposable BeginScope<TState>(TState state)
453480
454481 public void Log < TState > ( LogLevel logLevel , EventId eventId , TState state , Exception exception , Func < TState , Exception , string > formatter )
455482 {
456-
483+
457484 }
458485
459486 private class Scope : IDisposable
0 commit comments