@@ -16,6 +16,33 @@ public ClientHeaderEnricherTests()
1616 _contextAccessor = Substitute . For < IHttpContextAccessor > ( ) ;
1717 _contextAccessor . HttpContext . Returns ( httpContext ) ;
1818 }
19+
20+ [ Fact ]
21+ public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateNamedHeaderValueProperty ( )
22+ {
23+ // Arrange
24+ var headerKey = "RequestId" ;
25+ var propertyName = "HttpRequestId" ;
26+ var headerValue = Guid . NewGuid ( ) . ToString ( ) ;
27+ _contextAccessor . HttpContext . Request . Headers . Add ( headerKey , headerValue ) ;
28+
29+ var clientHeaderEnricher = new ClientHeaderEnricher ( headerKey , propertyName , _contextAccessor ) ;
30+
31+ LogEvent evt = null ;
32+ var log = new LoggerConfiguration ( )
33+ . Enrich . With ( clientHeaderEnricher )
34+ . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
35+ . CreateLogger ( ) ;
36+
37+ // Act
38+ log . Information ( @"First testing log enricher." ) ;
39+ log . Information ( @"Second testing log enricher." ) ;
40+
41+ // Assert
42+ Assert . NotNull ( evt ) ;
43+ Assert . True ( evt . Properties . ContainsKey ( propertyName ) ) ;
44+ Assert . Equal ( headerValue , evt . Properties [ propertyName ] . LiteralValue ( ) . ToString ( ) ) ;
45+ }
1946
2047 [ Fact ]
2148 public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateHeaderValueProperty ( )
@@ -25,7 +52,7 @@ public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateH
2552 var headerValue = Guid . NewGuid ( ) . ToString ( ) ;
2653 _contextAccessor . HttpContext . Request . Headers . Add ( headerKey , headerValue ) ;
2754
28- var clientHeaderEnricher = new ClientHeaderEnricher ( headerKey , _contextAccessor ) ;
55+ var clientHeaderEnricher = new ClientHeaderEnricher ( headerKey , propertyName : string . Empty , _contextAccessor ) ;
2956
3057 LogEvent evt = null ;
3158 var log = new LoggerConfiguration ( )
@@ -54,8 +81,8 @@ public void EnrichLogWithMulitpleClientHeaderEnricher_WhenHttpRequestContainHead
5481 _contextAccessor . HttpContext . Request . Headers . Add ( headerKey1 , headerValue1 ) ;
5582 _contextAccessor . HttpContext . Request . Headers . Add ( headerKey2 , headerValue2 ) ;
5683
57- var clientHeaderEnricher1 = new ClientHeaderEnricher ( headerKey1 , _contextAccessor ) ;
58- var clientHeaderEnricher2 = new ClientHeaderEnricher ( headerKey2 , _contextAccessor ) ;
84+ var clientHeaderEnricher1 = new ClientHeaderEnricher ( headerKey1 , propertyName : string . Empty , _contextAccessor ) ;
85+ var clientHeaderEnricher2 = new ClientHeaderEnricher ( headerKey2 , propertyName : string . Empty , _contextAccessor ) ;
5986
6087 LogEvent evt = null ;
6188 var log = new LoggerConfiguration ( )
@@ -81,7 +108,7 @@ public void EnrichLogWithClientHeader_WhenHttpRequestNotContainHeader_ShouldCrea
81108 {
82109 // Arrange
83110 var headerKey = "RequestId" ;
84- var clientHeaderEnricher = new ClientHeaderEnricher ( headerKey , _contextAccessor ) ;
111+ var clientHeaderEnricher = new ClientHeaderEnricher ( headerKey , propertyName : string . Empty , _contextAccessor ) ;
85112
86113 LogEvent evt = null ;
87114 var log = new LoggerConfiguration ( )
@@ -114,4 +141,4 @@ public void WithRequestHeader_ThenLoggerIsCalled_ShouldNotThrowException()
114141 // Assert
115142 Assert . Null ( exception ) ;
116143 }
117- }
144+ }
0 commit comments