From 0d8d4a63cdab205f085cec6318c481270d232dfd Mon Sep 17 00:00:00 2001 From: Praves Date: Fri, 22 Nov 2024 14:55:33 -0800 Subject: [PATCH] #243026895: Create Owin Stream only if needed. (#100) Co-authored-by: Keyur --- .../NetFramework/MoesifMiddlewareNetFramework.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs b/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs index 6865918..6575ac0 100644 --- a/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs +++ b/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs @@ -266,6 +266,7 @@ public void CreateStreamHelpers(IOwinContext httpContext, out StreamHelper outpu int parsedResContentLength = responseMaxBodySize - 1; GetContentLengthAndEncoding(resHeaders, parsedResContentLength, out parsedResContentLength); // Get the content-length from response header if possible. bool needToCreateStream = (logBody && parsedResContentLength <= responseMaxBodySize) ; + bool needToCreateStreamOwin = needToCreateStream; // We do not create Owin Stream if MVC stream is success. // Buffering mvc response HttpResponse httpResponse = HttpContext.Current?.Response; @@ -273,10 +274,13 @@ public void CreateStreamHelpers(IOwinContext httpContext, out StreamHelper outpu { outputCaptureMVC = new StreamHelper(httpResponse.Filter); httpResponse.Filter = outputCaptureMVC; + + // Check if we need to create Owin stream + needToCreateStreamOwin = (outputCaptureMVC == null || outputCaptureMVC.CopyStream.Length == 0); } // Create stream to buffer Owin response - if (needToCreateStream) + if (needToCreateStreamOwin) { IOwinResponse owinResponse = httpContext.Response; outputCaptureOwin = new StreamHelper(owinResponse.Body);