P0 任务 (立即执行): ✅ C++ 解析器增强: 改进正则解析,支持模板/命名空间/多重继承 ✅ 输入验证和安全加固: InputValidator 服务,代码大小限制,恶意代码检测 ✅ 缓存机制:CachedConversionService,SHA256 缓存键,60 分钟过期 P1 任务 (短期): ⏸️ 代码格式化集成:deferred (需要外部依赖) ⏸️ Web 界面暗黑模式:deferred (前端任务) ⏸️ 差异对比功能:deferred (前端任务) ✅ 日志增强:RequestLoggingMiddleware 中间件 新增文件: - CodePlay.Core/Parsers/CppParser.cs (增强版) - CodePlay.Core/Services/InputValidator.cs - CodePlay.Core/Services/CachedConversionService.cs - CodePlay.WebAPI/Middleware/RequestLoggingMiddleware.cs - CodePlay.Core/CodePlay.Core.csproj (新增 ClangSharp, MemoryCache 包) 测试结果: 42 个测试 (41 通过,1 跳过) ✅ 延后任务原因: - 代码格式化:需要安装 dotnet-format, google-java-format, clang-format - Web 界面功能:属于前端开发任务,需要 Vue3 开发 - 这些任务可以后续通过前端专项完成 Co-authored-by: monkeycode-ai <[email protected]>
77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
using Serilog.Context;
|
|
|
|
namespace CodePlay.WebAPI.Middleware;
|
|
|
|
/// <summary>
|
|
/// 请求日志中间件
|
|
/// 记录每个请求的详细信息
|
|
/// </summary>
|
|
public class RequestLoggingMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
private readonly ILogger<RequestLoggingMiddleware> _logger;
|
|
|
|
public RequestLoggingMiddleware(RequestDelegate next, ILogger<RequestLoggingMiddleware> logger)
|
|
{
|
|
_next = next;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext context)
|
|
{
|
|
var requestId = Guid.NewGuid().ToString("N")[..8];
|
|
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
|
|
|
// 添加请求上下文到日志
|
|
using (LogContext.PushProperty("RequestId", requestId))
|
|
using (LogContext.PushProperty("RequestMethod", context.Request.Method))
|
|
using (LogContext.PushProperty("RequestPath", context.Request.Path))
|
|
using (LogContext.PushProperty("UserAgent", context.Request.Headers.UserAgent.ToString()))
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation(
|
|
"请求开始 [{RequestMethod}] {RequestPath} (RequestID: {RequestId})",
|
|
context.Request.Method,
|
|
context.Request.Path,
|
|
requestId
|
|
);
|
|
|
|
await _next(context);
|
|
|
|
stopwatch.Stop();
|
|
_logger.LogInformation(
|
|
"请求完成 [{RequestMethod}] {RequestPath} - {StatusCode} - {ElapsedMs}ms (RequestID: {RequestId})",
|
|
context.Request.Method,
|
|
context.Request.Path,
|
|
context.Response.StatusCode,
|
|
stopwatch.ElapsedMilliseconds,
|
|
requestId
|
|
);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
stopwatch.Stop();
|
|
_logger.LogError(
|
|
ex,
|
|
"请求失败 [{RequestMethod}] {RequestPath} - {ElapsedMs}ms (RequestID: {RequestId})",
|
|
context.Request.Method,
|
|
context.Request.Path,
|
|
stopwatch.ElapsedMilliseconds,
|
|
requestId
|
|
);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 扩展方法
|
|
public static class RequestLoggingMiddlewareExtensions
|
|
{
|
|
public static IApplicationBuilder UseRequestLogging(this IApplicationBuilder builder)
|
|
{
|
|
return builder.UseMiddleware<RequestLoggingMiddleware>();
|
|
}
|
|
}
|