@@ -252,60 +252,65 @@ public static string EnsureTrailingSlash(string url)
252
252
return address . Uri . AbsoluteUri ;
253
253
}
254
254
255
- public static string GetHttpResponseLog ( string statusCode , IDictionary < string , IEnumerable < string > > headers , string body )
255
+ public static string GetHttpResponseLog ( string statusCode , IDictionary < string , IEnumerable < string > > headers , string body , IList < Regex > matchers = null )
256
256
{
257
257
StringBuilder httpResponseLog = new StringBuilder ( ) ;
258
258
httpResponseLog . AppendLine ( $ "============================ HTTP RESPONSE ============================{ Environment . NewLine } ") ;
259
259
httpResponseLog . AppendLine ( $ "Status Code:{ Environment . NewLine } { statusCode } { Environment . NewLine } ") ;
260
260
httpResponseLog . AppendLine ( $ "Headers:{ Environment . NewLine } { MessageHeadersToString ( headers ) } ") ;
261
- httpResponseLog . AppendLine ( $ "Body:{ Environment . NewLine } { TransformBody ( body ) } { Environment . NewLine } ") ;
261
+ httpResponseLog . AppendLine ( $ "Body:{ Environment . NewLine } { TransformBody ( body , matchers ) } { Environment . NewLine } ") ;
262
262
return httpResponseLog . ToString ( ) ;
263
263
}
264
264
265
- public static string GetHttpResponseLog ( string statusCode , HttpHeaders headers , string body )
265
+ public static string GetHttpResponseLog ( string statusCode , HttpHeaders headers , string body , IList < Regex > matchers = null )
266
266
{
267
- return GetHttpResponseLog ( statusCode , ConvertHttpHeadersToWebHeaderCollection ( headers ) , body ) ;
267
+ return GetHttpResponseLog ( statusCode , ConvertHttpHeadersToWebHeaderCollection ( headers ) , body , matchers ) ;
268
268
}
269
269
270
- public static string TransformBody ( string inBody )
270
+ public static string TransformBody ( string inBody , IList < Regex > matchers = null )
271
271
{
272
- Regex matcher = new Regex ( "(\\ s*\" access_token\" \\ s*:\\ s*)\" [^\" ]+\" " ) ;
273
- return matcher . Replace ( inBody , "$1\" <redacted>\" " ) ;
272
+ foreach ( Regex match in matchers )
273
+ {
274
+ inBody = match . Replace ( inBody , "$1\" <redacted>\" " ) ;
275
+ }
276
+ return inBody ;
274
277
}
275
278
276
279
public static string GetHttpRequestLog (
277
280
string method ,
278
281
string requestUri ,
279
282
IDictionary < string , IEnumerable < string > > headers ,
280
- string body )
283
+ string body ,
284
+ IList < Regex > matchers = null )
281
285
{
282
286
StringBuilder httpRequestLog = new StringBuilder ( ) ;
283
287
httpRequestLog . AppendLine ( string . Format ( "============================ HTTP REQUEST ============================{0}" , Environment . NewLine ) ) ;
284
288
httpRequestLog . AppendLine ( string . Format ( "HTTP Method:{0}{1}{0}" , Environment . NewLine , method ) ) ;
285
289
httpRequestLog . AppendLine ( string . Format ( "Absolute Uri:{0}{1}{0}" , Environment . NewLine , requestUri ) ) ;
286
290
httpRequestLog . AppendLine ( string . Format ( "Headers:{0}{1}" , Environment . NewLine , MessageHeadersToString ( headers ) ) ) ;
287
- httpRequestLog . AppendLine ( string . Format ( "Body:{0}{1}{0}" , Environment . NewLine , TransformBody ( body ) ) ) ;
291
+ httpRequestLog . AppendLine ( string . Format ( "Body:{0}{1}{0}" , Environment . NewLine , TransformBody ( body , matchers ) ) ) ;
288
292
289
293
return httpRequestLog . ToString ( ) ;
290
294
}
291
295
292
- public static string GetHttpRequestLog ( string method , string requestUri , HttpHeaders headers , string body )
296
+ public static string GetHttpRequestLog ( string method , string requestUri , HttpHeaders headers , string body , IList < Regex > matchers = null )
293
297
{
294
- return GetHttpRequestLog ( method , requestUri , ConvertHttpHeadersToWebHeaderCollection ( headers ) , body ) ;
298
+ return GetHttpRequestLog ( method , requestUri , ConvertHttpHeadersToWebHeaderCollection ( headers ) , body , matchers ) ;
295
299
}
296
300
297
- public static string GetLog ( HttpResponseMessage response )
301
+ public static string GetLog ( HttpResponseMessage response , IList < Regex > matchers = null )
298
302
{
299
303
string body = response . Content == null ? string . Empty
300
304
: FormatString ( response . Content . ReadAsStringAsync ( ) . Result ) ;
301
305
302
306
return GetHttpResponseLog (
303
307
response . StatusCode . ToString ( ) ,
304
308
response . Headers ,
305
- body ) ;
309
+ body ,
310
+ matchers ) ;
306
311
}
307
312
308
- public static string GetLog ( HttpRequestMessage request )
313
+ public static string GetLog ( HttpRequestMessage request , IList < Regex > matchers = null )
309
314
{
310
315
string body = request . Content == null ? string . Empty
311
316
: FormatString ( request . Content . ReadAsStringAsync ( ) . Result ) ;
@@ -314,7 +319,8 @@ public static string GetLog(HttpRequestMessage request)
314
319
request . Method . ToString ( ) ,
315
320
request . RequestUri . ToString ( ) ,
316
321
( HttpHeaders ) request . Headers ,
317
- body ) ;
322
+ body ,
323
+ matchers ) ;
318
324
}
319
325
320
326
public static string FormatString ( string content )
0 commit comments