.Net 6 API 在数据库中保存请求和响应日志

4 c# asp.net-web-api asp.net-core .net-6.0

我需要将我的请求和响应 JSON 保存在数据库中,到目前为止我还没有找到任何代码来执行此操作。

在较旧的.net core版本中,我使用以下代码来读取正文:

context.Request.EnableRewind();
Run Code Online (Sandbox Code Playgroud)

但似乎这段代码在.Net 6 中不起作用,或者我不知道使用它的技巧,这是我的最后一个代码,它能够读取响应,但请求始终为空。

public class APILogFilter : IActionFilter
{
    public async void OnActionExecuted(ActionExecutedContext context)
    {
        string RequestBody = "";
        context.HttpContext.Request.EnableBuffering();
        using (StreamReader reader = new StreamReader(context.HttpContext.Request.Body, Encoding.UTF8, true, 1024, true))
        {
            RequestBody = await reader.ReadToEndAsync();
        };

        string ResultBody = "";
        if (context.Result is ObjectResult)
        {
            var objResult = (ObjectResult)context.Result;
            ResultBody = HelperConvert.GetJSONSerialize(objResult.Value);
        }
        //Save request and response in database 
    }
    public void OnActionExecuting(ActionExecutingContext context)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

我希望有人能帮助我。

Geo*_*sov 6

Middleware我可以为您提供代替过滤器的使用。

该中间件在 .NET 6 上运行良好。

public class BodyLoggerMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ILogger<BodyLoggerMiddleware> _logger;

    public BodyLoggerMiddleware(RequestDelegate next, ILogger<BodyLoggerMiddleware> logger)
    {
        _next = next;
        _logger = logger;
    }

    public async Task Invoke(HttpContext context)
    {
        
        try
        {
            string requestBody;
            context.Request.EnableBuffering();
            using (StreamReader reader = new StreamReader(context.Request.Body, Encoding.UTF8, true, 1024, true))
            {
                requestBody = await reader.ReadToEndAsync();
            }
            _logger.LogInformation("Request: {Request}", requestBody);
        }
        finally
        {
            context.Request.Body.Position = 0;
        }
       

        Stream originalBody = context.Response.Body;

        try
        {
            using (var memStream = new MemoryStream())
            {
                context.Response.Body = memStream;

                await _next(context);

                memStream.Position = 0;
                string responseBody = new StreamReader(memStream).ReadToEnd();
                _logger.LogInformation("Response: {Response}", responseBody);
                memStream.Position = 0;
                await memStream.CopyToAsync(originalBody);
            }
        }
        finally
        {
            context.Response.Body = originalBody;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)