@@ -50,19 +50,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
5050 /// </summary>
5151 private async Task CaptureRequest ( HttpRequestMessage request , HttpResponseMessage ? response , Exception ? exception , long durationMs )
5252 {
53- var context = _httpContextAccessor . HttpContext ;
54-
55- if ( context == null )
56- {
57- return ;
58- }
59-
60- if ( ! context . Items . TryGetValue ( "DebugProbeEntry" , out var value ) )
61- {
62- return ;
63- }
64-
65- if ( value is not DebugEntry entry )
53+ if ( ! TryGetActiveEntry ( out var entry ) )
6654 {
6755 return ;
6856 }
@@ -88,34 +76,42 @@ private async Task CaptureRequest(HttpRequestMessage request, HttpResponseMessag
8876 ResponseHeaders = response != null ? response . Headers . ToDictionary ( x => x . Key , x => RedactionUtils . RedactHeader ( x . Key , string . Join ( ", " , x . Value ) , _options ) ) : [ ]
8977 } ;
9078
91- if ( request . Content != null )
92- {
93- var contentType = request . Content . Headers . ContentType ? . MediaType ;
79+ outgoing . RequestBody = await CaptureBodyAsync ( request . Content ) ;
9480
95- if ( HttpContentUtils . IsTextContent ( contentType ) )
96- {
97- var body = await request . Content . ReadAsStringAsync ( ) ;
81+ outgoing . ResponseBody = await CaptureBodyAsync ( response ? . Content ) ;
9882
99- outgoing . RequestBody = JsonUtils . Format ( RedactionUtils . RedactJsonFields (
100- HttpContentUtils . Trim ( body , _options . MaxBodyCaptureSizeBytes ) ,
101- _options ) ) ;
102- }
103- }
83+ entry . OutgoingRequests . Add ( outgoing ) ;
84+ }
85+
86+ private bool TryGetActiveEntry ( out DebugEntry entry )
87+ {
88+ entry = null ! ;
89+
90+ var context = _httpContextAccessor . HttpContext ;
10491
105- if ( response ? . Content != null )
92+ if ( context == null ||
93+ ! context . Items . TryGetValue ( "DebugProbeEntry" , out var value ) ||
94+ value is not DebugEntry activeEntry )
10695 {
107- var contentType = response . Content . Headers . ContentType ? . MediaType ;
96+ return false ;
97+ }
10898
109- if ( HttpContentUtils . IsTextContent ( contentType ) )
110- {
111- var body = await response . Content . ReadAsStringAsync ( ) ;
99+ entry = activeEntry ;
100+ return true ;
101+ }
112102
113- outgoing . ResponseBody = JsonUtils . Format ( RedactionUtils . RedactJsonFields (
114- HttpContentUtils . Trim ( body , _options . MaxBodyCaptureSizeBytes ) ,
115- _options ) ) ;
116- }
103+ private async Task < string > CaptureBodyAsync ( HttpContent ? content )
104+ {
105+ if ( content == null ||
106+ ! HttpContentUtils . IsTextContent ( content . Headers . ContentType ? . MediaType ) )
107+ {
108+ return string . Empty ;
117109 }
118110
119- entry . OutgoingRequests . Add ( outgoing ) ;
111+ var body = await content . ReadAsStringAsync ( ) ;
112+
113+ return JsonUtils . Format ( RedactionUtils . RedactJsonFields (
114+ HttpContentUtils . Trim ( body , _options . MaxBodyCaptureSizeBytes ) ,
115+ _options ) ) ;
120116 }
121117}
0 commit comments