😊 修复 HTTP 远程请求分析工具存在重复打印问题

This commit is contained in:
MonkSoul
2025-12-03 16:01:13 +08:00
parent fdff63612d
commit 82091b43cd
6 changed files with 42 additions and 6 deletions

View File

@@ -96,6 +96,12 @@ internal static class Constants
/// <remarks>被用于从 <see cref="HttpRequestMessage" /> 的 <c>Options</c> 属性中读取。</remarks>
internal const string DISABLE_PROFILER_KEY = "__DISABLE_PROFILER__";
/// <summary>
/// 请求分析工具打印标识键
/// </summary>
/// <remarks>解决重复打印问题。被用于从 <see cref="HttpRequestMessage" /> 的 <c>Options</c> 属性中读取。</remarks>
internal const string PROFILER_PRINTED_KEY = "__PROFILER_PRINTED__";
/// <summary>
/// 启用 JSON 响应反序列化包装器键
/// </summary>

View File

@@ -55,9 +55,18 @@ public sealed class ProfilerDelegatingHandler(IHttpRemoteLogger logger, IOptions
/// <returns>
/// <see cref="bool" />
/// </returns>
internal static bool IsEnabled(HttpRequestMessage httpRequestMessage) =>
!(httpRequestMessage.Options.TryGetValue(new HttpRequestOptionsKey<string>(Constants.DISABLE_PROFILER_KEY),
out var value) && value == "TRUE");
internal static bool IsEnabled(HttpRequestMessage httpRequestMessage)
{
// 检查是否已打印过
if (httpRequestMessage.Options.TryGetValue(new HttpRequestOptionsKey<string>(Constants.PROFILER_PRINTED_KEY),
out _))
{
return false;
}
return !(httpRequestMessage.Options.TryGetValue(
new HttpRequestOptionsKey<string>(Constants.DISABLE_PROFILER_KEY), out var value) && value == "TRUE");
}
/// <inheritdoc />
protected override HttpResponseMessage Send(HttpRequestMessage httpRequestMessage,

View File

@@ -456,6 +456,9 @@ internal sealed partial class HttpRemoteService : IHttpRemoteService
// 检查是否启用请求分析工具
if (httpRequestBuilder.ProfilerEnabled)
{
// 标记已打印,解决重复打印问题
httpRequestMessage.Options.TryAdd(Constants.PROFILER_PRINTED_KEY, "TRUE");
// 初始化 HttpRemoteAnalyzer 实例
httpRemoteAnalyzer = httpRequestBuilder.ProfilerPredicate is not null ? new HttpRemoteAnalyzer() : null;

View File

@@ -96,6 +96,12 @@ internal static class Constants
/// <remarks>被用于从 <see cref="HttpRequestMessage" /> 的 <c>Options</c> 属性中读取。</remarks>
internal const string DISABLE_PROFILER_KEY = "__DISABLE_PROFILER__";
/// <summary>
/// 请求分析工具打印标识键
/// </summary>
/// <remarks>解决重复打印问题。被用于从 <see cref="HttpRequestMessage" /> 的 <c>Options</c> 属性中读取。</remarks>
internal const string PROFILER_PRINTED_KEY = "__PROFILER_PRINTED__";
/// <summary>
/// 启用 JSON 响应反序列化包装器键
/// </summary>

View File

@@ -55,9 +55,18 @@ public sealed class ProfilerDelegatingHandler(IHttpRemoteLogger logger, IOptions
/// <returns>
/// <see cref="bool" />
/// </returns>
internal static bool IsEnabled(HttpRequestMessage httpRequestMessage) =>
!(httpRequestMessage.Options.TryGetValue(new HttpRequestOptionsKey<string>(Constants.DISABLE_PROFILER_KEY),
out var value) && value == "TRUE");
internal static bool IsEnabled(HttpRequestMessage httpRequestMessage)
{
// 检查是否已打印过
if (httpRequestMessage.Options.TryGetValue(new HttpRequestOptionsKey<string>(Constants.PROFILER_PRINTED_KEY),
out _))
{
return false;
}
return !(httpRequestMessage.Options.TryGetValue(
new HttpRequestOptionsKey<string>(Constants.DISABLE_PROFILER_KEY), out var value) && value == "TRUE");
}
/// <inheritdoc />
protected override HttpResponseMessage Send(HttpRequestMessage httpRequestMessage,

View File

@@ -456,6 +456,9 @@ internal sealed partial class HttpRemoteService : IHttpRemoteService
// 检查是否启用请求分析工具
if (httpRequestBuilder.ProfilerEnabled)
{
// 标记已打印,解决重复打印问题
httpRequestMessage.Options.TryAdd(Constants.PROFILER_PRINTED_KEY, "TRUE");
// 初始化 HttpRemoteAnalyzer 实例
httpRemoteAnalyzer = httpRequestBuilder.ProfilerPredicate is not null ? new HttpRemoteAnalyzer() : null;