Compare commits
3
Commits
10.26.9.1
...
e4f051cb61
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e4f051cb61 | ||
|
|
7b34991ea0 | ||
|
|
1623343748 |
No files matched your search
@@ -16,4 +16,9 @@ public class SimApiRequestLogOptions
|
|||||||
/// 请求字段显示最长长度
|
/// 请求字段显示最长长度
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int RequestStringLogLength { get; set; } = 0;
|
public int RequestStringLogLength { get; set; } = 0;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 响应字段显示最长长度
|
||||||
|
/// </summary>
|
||||||
|
public int ResponseStringLogLength { get; set; } = 0;
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ using System.Linq;
|
|||||||
using System.Runtime.ExceptionServices;
|
using System.Runtime.ExceptionServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@@ -61,35 +62,8 @@ public class SimApiRequestLogMiddleware(
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// GET/DELETE 等无 body 的请求, 或表单/文件上传等非 JSON body:
|
// GET/DELETE 等无 body 的请求, 或表单/文件上传等非 JSON body:
|
||||||
// 不进行长度裁剪, 原样记录, 避免空 JSON 解析抛异常
|
// 不做长度裁剪, 原样记录, 避免空 JSON 解析抛异常
|
||||||
Dictionary<string, object>? reqLogs = null;
|
logMessage.AppendLine(TrimJsonStringFields(requestBodyText, options.RequestStringLogLength));
|
||||||
try
|
|
||||||
{
|
|
||||||
reqLogs = SimApiUtil.FromJson<Dictionary<string, object>>(requestBodyText);
|
|
||||||
}
|
|
||||||
catch (JsonException)
|
|
||||||
{
|
|
||||||
reqLogs = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (reqLogs == null)
|
|
||||||
{
|
|
||||||
logMessage.AppendLine(requestBodyText);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
foreach (var reqLog in reqLogs)
|
|
||||||
{
|
|
||||||
if (reqLog.Value is not JsonElement { ValueKind: JsonValueKind.String } je) continue;
|
|
||||||
var str = je.GetString();
|
|
||||||
if (str?.Length > options.RequestStringLogLength)
|
|
||||||
{
|
|
||||||
reqLogs[reqLog.Key] = str[..options.RequestStringLogLength] + $"...({str.Length})";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logMessage.AppendLine(SimApiUtil.Json(reqLogs));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var originalBodyStream = context.Response.Body;
|
var originalBodyStream = context.Response.Body;
|
||||||
@@ -114,7 +88,12 @@ public class SimApiRequestLogMiddleware(
|
|||||||
var responseText = await new StreamReader(responseBody).ReadToEndAsync();
|
var responseText = await new StreamReader(responseBody).ReadToEndAsync();
|
||||||
|
|
||||||
logMessage.AppendLine($"*( Response [{context.Response.StatusCode}] ) =>");
|
logMessage.AppendLine($"*( Response [{context.Response.StatusCode}] ) =>");
|
||||||
if (options.ShowFullResponse)
|
if (options.ResponseStringLogLength > 0)
|
||||||
|
{
|
||||||
|
// 与请求体相同的字段级裁剪: JSON 按字段截断超长字符串, 非 JSON/空响应原样记录
|
||||||
|
logMessage.Append(TrimJsonStringFields(responseText, options.ResponseStringLogLength));
|
||||||
|
}
|
||||||
|
else if (options.ShowFullResponse)
|
||||||
{
|
{
|
||||||
logMessage.Append(responseText);
|
logMessage.Append(responseText);
|
||||||
}
|
}
|
||||||
@@ -139,4 +118,79 @@ public class SimApiRequestLogMiddleware(
|
|||||||
|
|
||||||
edi?.Throw();
|
edi?.Throw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 与请求体相同的字段级裁剪(递归):
|
||||||
|
/// 遍历 JSON 对象/数组的所有层级, 把长度超过 maxLength 的字符串字段截断为前 maxLength 字符并追加 ...(原长度);
|
||||||
|
/// 空 body 或非 JSON body(表单/文件上传等)原样返回, 不裁剪
|
||||||
|
/// </summary>
|
||||||
|
private static string TrimJsonStringFields(string text, int maxLength)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(text))
|
||||||
|
{
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonNode? root;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
root = JsonNode.Parse(text);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (root == null)
|
||||||
|
{
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
TrimNode(root, maxLength);
|
||||||
|
return SimApiUtil.Json(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 递归裁剪节点下所有层级的字符串值:
|
||||||
|
/// 纯字符串字面量超长时截断, 数字/布尔/嵌套对象/数组继续向下递归
|
||||||
|
/// </summary>
|
||||||
|
private static void TrimNode(JsonNode? node, int maxLength)
|
||||||
|
{
|
||||||
|
switch (node)
|
||||||
|
{
|
||||||
|
case JsonObject obj:
|
||||||
|
// 键快照遍历后再赋值, 避免枚举过程中修改集合引发异常
|
||||||
|
foreach (var prop in obj.ToList())
|
||||||
|
{
|
||||||
|
if (prop.Value is JsonValue value &&
|
||||||
|
value.TryGetValue<string>(out var str) &&
|
||||||
|
str.Length > maxLength)
|
||||||
|
{
|
||||||
|
obj[prop.Key] = JsonValue.Create(str[..maxLength] + $"...({str.Length})");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TrimNode(prop.Value, maxLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case JsonArray arr:
|
||||||
|
for (var i = 0; i < arr.Count; i++)
|
||||||
|
{
|
||||||
|
if (arr[i] is JsonValue value &&
|
||||||
|
value.TryGetValue<string>(out var str) &&
|
||||||
|
str.Length > maxLength)
|
||||||
|
{
|
||||||
|
arr[i] = JsonValue.Create(str[..maxLength] + $"...({str.Length})");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TrimNode(arr[i], maxLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+1
-2
@@ -353,8 +353,7 @@ public static class SimApiExtensions
|
|||||||
builder.Configure<ForwardedHeadersOptions>(fwOptions =>
|
builder.Configure<ForwardedHeadersOptions>(fwOptions =>
|
||||||
{
|
{
|
||||||
fwOptions.ForwardedHeaders = ForwardedHeaders.All;
|
fwOptions.ForwardedHeaders = ForwardedHeaders.All;
|
||||||
fwOptions.KnownIPNetworks.Clear();
|
fwOptions.KnownNetworks.Clear();
|
||||||
fwOptions.KnownProxies.Clear();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user