Skip to content

Commit 78855dc

Browse files
committed
handle "has logged" case in the response stream
1 parent e21402b commit 78855dc

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/Middleware/HttpLogging/src/ResponseBufferingStream.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal sealed class ResponseBufferingStream : BufferingStream, IHttpResponseBo
1919
private readonly HttpLoggingOptions _options;
2020
private readonly IHttpLoggingInterceptor[] _interceptors;
2121
private bool _logBody;
22+
private bool _hasLogged;
2223
private Encoding? _encoding;
2324
private string? _bodyBeforeClose;
2425

@@ -182,6 +183,7 @@ public void LogResponseBody()
182183
{
183184
var responseBody = GetStringInternal();
184185
_logger.ResponseBody(responseBody);
186+
_hasLogged = true;
185187
}
186188
}
187189

@@ -190,6 +192,7 @@ public void LogResponseBody(HttpLoggingInterceptorContext logContext)
190192
if (_logBody)
191193
{
192194
logContext.AddParameter("ResponseBody", GetStringInternal());
195+
_hasLogged = true;
193196
}
194197
}
195198

@@ -203,7 +206,7 @@ private string GetStringInternal()
203206

204207
public override void Close()
205208
{
206-
if (_logBody)
209+
if (_logBody && !_hasLogged)
207210
{
208211
// Subsequent middleware can close the response stream after writing its body
209212
// Preserving the body for the final GetStringInternal() call.

0 commit comments

Comments
 (0)