@@ -127,29 +127,48 @@ static void InitDefaultContextProvider()
127127
128128 var contextType = httpContext . GetType ( ) ;
129129
130+ // User info
130131 var user = contextType . GetProperty ( "User" ) ? . GetValue ( httpContext ) as ClaimsPrincipal ;
131132 var userId = user ? . GetId ( ) ;
133+ var userEmail = user ? . GetEmail ( ) ;
132134
135+ // Request info
133136 var request = contextType . GetProperty ( "Request" ) ? . GetValue ( httpContext ) ;
134- string requestUrl = null ;
137+ string requestUrl = null , httpMethod = null , userAgent = null , traceId = null ;
135138 if ( request != null )
136139 {
137140 var reqType = request . GetType ( ) ;
138141 var pathBase = reqType . GetProperty ( "PathBase" ) ? . GetValue ( request ) ? . ToString ( ) ;
139142 var path = reqType . GetProperty ( "Path" ) ? . GetValue ( request ) ? . ToString ( ) ;
140143 var queryString = reqType . GetProperty ( "QueryString" ) ? . GetValue ( request ) ? . ToString ( ) ;
141144 requestUrl = $ "{ pathBase } { path } { queryString } ";
145+ httpMethod = reqType . GetProperty ( "Method" ) ? . GetValue ( request ) ? . ToString ( ) ;
146+
147+ var headers = reqType . GetProperty ( "Headers" ) ? . GetValue ( request ) ;
148+ if ( headers != null )
149+ {
150+ var indexer = headers . GetType ( ) . GetProperty ( "Item" , new [ ] { typeof ( string ) } ) ;
151+ userAgent = indexer ? . GetValue ( headers , new object [ ] { "User-Agent" } ) ? . ToString ( ) ;
152+ }
142153 }
143154
155+ // Connection info
144156 var connection = contextType . GetProperty ( "Connection" ) ? . GetValue ( httpContext ) ;
145157 var userIp = connection ? . GetType ( ) . GetProperty ( "RemoteIpAddress" ) ? . GetValue ( connection ) ? . ToString ( ) ;
146158
159+ // Trace identifier
160+ traceId = contextType . GetProperty ( "TraceIdentifier" ) ? . GetValue ( httpContext ) ? . ToString ( ) ;
161+
147162 if ( userId . IsEmpty ( ) && requestUrl . IsEmpty ( ) && userIp . IsEmpty ( ) ) return null ;
148163
149164 var r = new StringBuilder ( ) ;
150165 if ( userId . HasValue ( ) ) r . AppendLine ( $ " UserId: { userId } ") ;
166+ if ( userEmail . HasValue ( ) ) r . AppendLine ( $ " UserEmail: { userEmail } ") ;
167+ if ( httpMethod . HasValue ( ) ) r . AppendLine ( $ " HttpMethod: { httpMethod } ") ;
151168 if ( requestUrl . HasValue ( ) ) r . AppendLine ( $ " RequestUrl: { requestUrl } ") ;
152169 if ( userIp . HasValue ( ) ) r . AppendLine ( $ " UserIP: { userIp } ") ;
170+ if ( userAgent . HasValue ( ) ) r . AppendLine ( $ " UserAgent: { userAgent } ") ;
171+ if ( traceId . HasValue ( ) ) r . AppendLine ( $ " TraceId: { traceId } ") ;
153172 return r . ToString ( ) . TrimEnd ( ) ;
154173 }
155174 catch { return null ; }
0 commit comments