@@ -19,32 +19,62 @@ public LoggingEnricherMiddleware(RequestDelegate next, ILogger<LoggingEnricherMi
1919
2020 public async Task InvokeAsync ( HttpContext context )
2121 {
22+ // Cache Activity reference to avoid multiple property accesses
23+ var activity = Activity . Current ;
24+
25+ // Get headers once to avoid multiple lookups
26+ var headers = context . Request . Headers ;
27+
2228 // Get correlation ID (already set by MartenMetadataMiddleware)
23- var correlationId = context . Request . Headers [ "X-Correlation-ID" ] . FirstOrDefault ( )
24- ?? Activity . Current ? . RootId
25- ?? Guid . CreateVersion7 ( ) . ToString ( ) ;
29+ var correlationId = headers . TryGetValue ( "X-Correlation-ID" , out var correlationHeader )
30+ ? correlationHeader . ToString ( )
31+ : activity ? . RootId ?? Guid . CreateVersion7 ( ) . ToString ( ) ;
2632
27- // Get trace and span IDs from OpenTelemetry Activity
28- var traceId = Activity . Current ? . TraceId . ToString ( ) ;
29- var spanId = Activity . Current ? . SpanId . ToString ( ) ;
33+ // Get causation ID from header
34+ var causationId = headers . TryGetValue ( "X-Causation-ID" , out var causationHeader )
35+ ? causationHeader . ToString ( )
36+ : activity ? . ParentId ;
37+
38+ // Get trace and span IDs from OpenTelemetry Activity (avoid ToString() if null)
39+ var traceId = activity ? . TraceId . ToString ( ) ;
40+ var spanId = activity ? . SpanId . ToString ( ) ;
3041
3142 // Get user information (excluding email for privacy)
32- var userId = context . User ? . Identity ? . Name ?? "anonymous" ;
43+ var userId = context . User . Identity ? . Name ?? "anonymous" ;
3344
34- // Get request information
35- var requestPath = context . Request . Path . Value ;
36- var requestMethod = context . Request . Method ;
37- var userAgent = context . Request . Headers [ "User-Agent" ] . FirstOrDefault ( ) ;
45+ // Get request information (cache frequently accessed properties)
46+ var request = context . Request ;
47+ var requestPath = request . Path . Value ;
48+ var requestMethod = request . Method ;
49+ var userAgent = headers . TryGetValue ( "User-Agent" , out var userAgentHeader )
50+ ? userAgentHeader . ToString ( )
51+ : null ;
3852 var remoteIp = context . Connection . RemoteIpAddress ? . ToString ( ) ;
3953
40- // Log the request start with enriched metadata
41- Log . Infrastructure . RequestStarted (
42- _logger ,
43- requestMethod ,
44- requestPath ?? "/" ,
45- remoteIp ?? "unknown" ) ;
54+ // Create a log scope with all metadata - this makes these properties available
55+ // in all structured logs within this request
56+ using ( _logger . BeginScope ( new Dictionary < string , object ? >
57+ {
58+ [ "CorrelationId" ] = correlationId ,
59+ [ "CausationId" ] = causationId ,
60+ [ "TraceId" ] = traceId ,
61+ [ "SpanId" ] = spanId ,
62+ [ "UserId" ] = userId ,
63+ [ "RequestPath" ] = requestPath ,
64+ [ "RequestMethod" ] = requestMethod ,
65+ [ "RemoteIp" ] = remoteIp ,
66+ [ "UserAgent" ] = userAgent
67+ } ) )
68+ {
69+ // Log the request start with enriched metadata
70+ Log . Infrastructure . RequestStarted (
71+ _logger ,
72+ requestMethod ,
73+ requestPath ?? "/" ,
74+ remoteIp ?? "unknown" ) ;
4675
47- await _next ( context ) ;
76+ await _next ( context ) ;
77+ }
4878 }
4979}
5080
0 commit comments