添加更多上下文方便调试
This commit is contained in:
parent
9344ef6ccf
commit
c4fbd572cc
4 changed files with 157 additions and 2 deletions
|
@ -152,5 +152,66 @@ namespace iFileProxy.Helpers
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从HttpContext中提取调试信息
|
||||||
|
/// </summary>
|
||||||
|
public static async Task<HttpContextDebugInfo> ExtractDebugInfo(HttpContext context)
|
||||||
|
{
|
||||||
|
var debugInfo = new HttpContextDebugInfo
|
||||||
|
{
|
||||||
|
RequestId = context.TraceIdentifier,
|
||||||
|
Path = context.Request.Path,
|
||||||
|
Method = context.Request.Method,
|
||||||
|
ClientIP = GetClientIPAddr(context),
|
||||||
|
UserAgent = context.Request.Headers.UserAgent.ToString(),
|
||||||
|
Host = context.Request.Host.ToString(),
|
||||||
|
ContentType = context.Request.ContentType ?? string.Empty,
|
||||||
|
ContentLength = context.Request.ContentLength,
|
||||||
|
IsHttps = context.Request.IsHttps,
|
||||||
|
Headers = context.Request.Headers.ToDictionary(
|
||||||
|
h => h.Key,
|
||||||
|
h => h.Value.ToArray(),
|
||||||
|
StringComparer.OrdinalIgnoreCase
|
||||||
|
),
|
||||||
|
QueryParams = context.Request.Query.ToDictionary(
|
||||||
|
q => q.Key,
|
||||||
|
q => q.Value.ToArray(),
|
||||||
|
StringComparer.OrdinalIgnoreCase
|
||||||
|
),
|
||||||
|
Cookies = context.Request.Cookies.ToDictionary(
|
||||||
|
c => c.Key,
|
||||||
|
c => c.Value,
|
||||||
|
StringComparer.OrdinalIgnoreCase
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
// 获取表单数据
|
||||||
|
if (context.Request.HasFormContentType)
|
||||||
|
{
|
||||||
|
var form = await context.Request.ReadFormAsync();
|
||||||
|
debugInfo.FormData = form.ToDictionary(
|
||||||
|
f => f.Key,
|
||||||
|
f => f.Value.ToArray(),
|
||||||
|
StringComparer.OrdinalIgnoreCase
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取请求体
|
||||||
|
if (context.Request.Body.CanRead)
|
||||||
|
{
|
||||||
|
context.Request.EnableBuffering();
|
||||||
|
using var reader = new StreamReader(
|
||||||
|
context.Request.Body,
|
||||||
|
encoding: System.Text.Encoding.UTF8,
|
||||||
|
detectEncodingFromByteOrderMarks: false,
|
||||||
|
leaveOpen: true
|
||||||
|
);
|
||||||
|
debugInfo.RequestBody = await reader.ReadToEndAsync();
|
||||||
|
context.Request.Body.Position = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return debugInfo;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ namespace iFileProxy
|
||||||
if (context.Response.StatusCode == 404)
|
if (context.Response.StatusCode == 404)
|
||||||
{
|
{
|
||||||
context.Response.ContentType = "application/json";
|
context.Response.ContentType = "application/json";
|
||||||
await context.Response.WriteAsync(JsonSerializer.Serialize(new CommonRsp { Retcode = 404, Message = "this route not exists." }));
|
await context.Response.WriteAsync( JsonSerializer.Serialize(new CommonRsp { Retcode = 404, Message = "this route not exists." }));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ namespace iFileProxy
|
||||||
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
|
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||||
{
|
{
|
||||||
var code = HttpStatusCode.InternalServerError; // 500 if unexpected
|
var code = HttpStatusCode.InternalServerError; // 500 if unexpected
|
||||||
_logger.Error("Crash Data: {exception}",exception);
|
_logger.Error("Crash Data: {exception}\nContext: {context}", exception, JsonSerializer.Serialize (MasterHelper.ExtractDebugInfo(context)));
|
||||||
switch (exception)
|
switch (exception)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
93
src/Models/HttpContextDebugInfo.cs
Normal file
93
src/Models/HttpContextDebugInfo.cs
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace iFileProxy.Models
|
||||||
|
{
|
||||||
|
public class HttpContextDebugInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 请求ID
|
||||||
|
/// </summary>
|
||||||
|
public string RequestId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 请求路径
|
||||||
|
/// </summary>
|
||||||
|
public string Path { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 请求方法
|
||||||
|
/// </summary>
|
||||||
|
public string Method { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 客户端IP
|
||||||
|
/// </summary>
|
||||||
|
public string ClientIP { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// User-Agent
|
||||||
|
/// </summary>
|
||||||
|
public string UserAgent { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 请求头信息
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<string, string[]> Headers { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查询参数
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<string, string[]> QueryParams { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 表单数据
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<string, string[]> FormData { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Cookie信息
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<string, string> Cookies { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 请求体内容
|
||||||
|
/// </summary>
|
||||||
|
public string? RequestBody { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 请求时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime RequestTime { get; set; } = DateTime.Now;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 服务器主机名
|
||||||
|
/// </summary>
|
||||||
|
public string Host { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 请求内容类型
|
||||||
|
/// </summary>
|
||||||
|
public string ContentType { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 内容长度
|
||||||
|
/// </summary>
|
||||||
|
public long? ContentLength { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否是HTTPS
|
||||||
|
/// </summary>
|
||||||
|
public bool IsHttps { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 转换为格式化的字符串
|
||||||
|
/// </summary>
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize(this, new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
WriteIndented = true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using iFileProxy.Sinks;
|
using iFileProxy.Sinks;
|
||||||
using iFileProxy.Services;
|
using iFileProxy.Services;
|
||||||
|
using ZstdSharp.Unsafe;
|
||||||
|
|
||||||
public static class SerilogConfig
|
public static class SerilogConfig
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue