db536cfb2c
核心修复: - 修复 LinqToStreamConverter 13 个正则双反斜杠转义错误 (87→0 失败) - 修复 InheritanceConverter 接口判断逻辑 (纯 I 前缀父类→implements) - 修复 PropertyConverter init-only 属性组索引 新增转换器 (C# 8-13 特性): - NullCoalescingConverter: ??、?.、??= 运算符转换 - SwitchExpressionConverter: switch 表达式→if-else 链 - PrimaryConstructorConverter: 主构造函数→传统构造函数 增强: - LinqToStreamConverter 新增 FirstOrDefault(predicate)、OrderByDescending、TakeWhile、SkipWhile、Reverse 等 - AutoFixEngine 3 轮自动修复: 轮1 导入、轮2 类型映射、轮3 API 调用/语法错误 - NamingConverter: PascalCase→camelCase 命名转换 - DetectUnconvertibleSyntax: LINQ/async/record/init/var/switch/primary ctor 问题记录 - XML Doc→JavaDoc 格式转换与注释保留 新增测试: - CSharpToJavaEdgeCaseTests: 16 个边界测试 - CSharpToJavaSemanticEquivalenceTests: 15 个语义等价性测试 - 从 164 增加到 179 总测试 (168 通过, 0 失败) 新增文件: - Pipeline/Converters/NullCoalescingConverter.cs - Pipeline/Converters/SwitchExpressionConverter.cs - Pipeline/Converters/PrimaryConstructorConverter.cs - Converters/CSharpToCppStrategy.cs + CppCodeGenerator.cs - Tests/Semantics/CSharpToJavaSemanticEquivalenceTests.cs - Tests/CSharpAdvancedFeaturesTests.cs + CSharp13FeatureTests.cs Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
79 lines
2.6 KiB
C#
79 lines
2.6 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
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>();
|
|
}
|
|
}
|