From 05282cb0da02f838ec271e0e53a58568fda33182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Tue, 22 Oct 2024 13:44:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A1=AE=E4=BF=9D=E5=86=85=E5=AD=98=E6=B1=A0?= =?UTF-8?q?=E5=9B=9E=E6=94=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Stardust.Extensions/TracerMiddleware.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Stardust.Extensions/TracerMiddleware.cs b/Stardust.Extensions/TracerMiddleware.cs index 0e1d8c45..065d9927 100644 --- a/Stardust.Extensions/TracerMiddleware.cs +++ b/Stardust.Extensions/TracerMiddleware.cs @@ -49,25 +49,28 @@ public async Task Invoke(HttpContext ctx) { var flag = false; if (req.ContentLength != null && - req.ContentLength < 1024 * 8 && + req.ContentLength < 1024 && req.ContentType != null && req.ContentType.StartsWithIgnoreCase(TagTypes)) { + var buf = Pool.Shared.Rent(1024); try { req.EnableBuffering(); - var buf = Pool.Shared.Rent(1024); var count = await req.Body.ReadAsync(buf, 0, buf.Length); span.AppendTag("\r\n<=\r\n" + buf.ToStr(null, 0, count)); req.Body.Position = 0; - Pool.Shared.Return(buf); flag = true; } catch (Exception ex) { XTrace.Log.Error("[{0}]读取请求主体失败:{1}", action, ex.Message); } + finally + { + Pool.Shared.Return(buf); + } } if (span.Tag.Length < 500) @@ -107,25 +110,28 @@ public async Task Invoke(HttpContext ctx) { var flag = false; if (res.ContentLength != null && - res.ContentLength < 1024 * 8 && + res.ContentLength < 1024 && res.Body.CanSeek && res.ContentType != null && res.ContentType.StartsWithIgnoreCase(TagTypes)) { + var buf = Pool.Shared.Rent(1024); try { - var buf = Pool.Shared.Rent(1024); var p = res.Body.Position; var count = await res.Body.ReadAsync(buf, 0, buf.Length); span.AppendTag("\r\n=>\r\n" + buf.ToStr(null, 0, count)); res.Body.Position = p; - Pool.Shared.Return(buf); flag = true; } catch (Exception ex) { XTrace.Log.Error("[{0}]读取响应主体失败:{1}", action, ex.Message); } + finally + { + Pool.Shared.Return(buf); + } } if (span.Tag == null || span.Tag.Length < 500)