Repository: dotnet/aspnetcore
File: src/Middleware/HttpLogging/src/HttpLoggingMiddleware.cs (C#)

Code before:
                {
                    // If the middleware pipeline didn't read until 0 was returned from ReadAsync,
                    // make sure we log the request body.
                    requestBufferingStream?.LogRequestBody();

                    if (ResponseHeadersNotYetWritten(responseBufferingStream, loggableUpgradeFeature))
                    {
                        // No body, not an upgradable request or request not upgraded, write headers here.
                        await LogResponseHeadersAsync(logContext, options, _interceptors, _logger);
                    }
                    else
                    {
                        // There will only be a response body if the headers were already written.
                        responseBufferingStream?.LogResponseBody();
                    }

                    if (logContext.IsAnyEnabled(HttpLoggingFields.Duration))
                    {
                        _logger.Duration(logContext.GetDuration());
                    }
                }
            }
        }
        finally
        {
            if (responseBufferingStream != null)
            {
                _responseBufferingStreamPool.Return(responseBufferingStream);
            }

            if (originalBodyFeature != null)
            {
                context.Features.Set(originalBodyFeature);
            }

            requestBufferingStream?.Dispose();

            if (originalBody != null)
            {
                context.Request.Body = originalBody;
            }

            if (loggableUpgradeFeature != null)
            {
                context.Features.Set(originalUpgradeFeature);
            }

            logContext.Reset();
            _contextPool.Return(logContext);
        }
    }

Review comment (suggestion):
```suggestion
            if (responseBufferingStream is not null)
```

Code after:
                {
                    // If the middleware pipeline didn't read until 0 was returned from ReadAsync,
                    // make sure we log the request body.
                    requestBufferingStream?.LogRequestBody();

                    if (ResponseHeadersNotYetWritten(responseBufferingStream, loggableUpgradeFeature))
                    {
                        // No body, not an upgradable request or request not upgraded, write headers here.
                        await LogResponseHeadersAsync(logContext, options, _interceptors, _logger);
                    }
                    else
                    {
                        // There will only be a response body if the headers were already written.
                        responseBufferingStream?.LogResponseBody();
                    }

                    if (logContext.IsAnyEnabled(HttpLoggingFields.Duration))
                    {
                        _logger.Duration(logContext.GetDuration());
                    }
                }
            }
        }
        finally
        {
            if (responseBufferingStream is not null)
            {
                _responseBufferingStreamPool.Return(responseBufferingStream);
            }

            if (originalBodyFeature is not null)
            {
                context.Features.Set(originalBodyFeature);
            }

            requestBufferingStream?.Dispose();

            if (originalBody is not null)
            {
                context.Request.Body = originalBody;
            }

            if (loggableUpgradeFeature is not null)
            {
                context.Features.Set(originalUpgradeFeature);
            }

            logContext.Reset();
            _contextPool.Return(logContext);
        }
    }