【问题标题】:Intercepting HttpRequest and log contents to a database拦截 HttpRequest 并将内容记录到数据库
【发布时间】:2023-01-12 17:10:12
【问题描述】:

我想拦截所有 Httprequest 并将内容记录到数据库表中。我在 vs 中使用 EF core、swagger 和 c#。我还想添加一个后台工作程序,每 7 天删除一次此请求。

我试过使用 RequestDelegate 但我无法将信息保存到数据库中

【问题讨论】:

  • 你到底想记录什么?你使用什么框架?您使用的是 .NET Framework 吗? .NET 核心? .NET 5、6、7?你试过什么代码?为什么它不起作用?等等等等
  • 我们需要更多信息。你使用哪个框架?你在使用 ASP.NET Core 吗?如果是这样,中间件是您最好的选择。您想记录哪些数据?知识产权?时间?
  • 请提供足够的代码,以便其他人可以更好地理解或重现问题。
  • 我正在使用 .NET Core 6

标签: c# database entity-framework middleware


【解决方案1】:

你的问题有点太宽泛,不适合这个论坛,但这里至少是第一部分的一个例子——一个请求记录器,它将请求的 uri 和正文保存到数据库表中;

public class RequestMessageLogger : IRequestMessageLogger
{
    private readonly MyDbContext _dbContext;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public RequestMessageLogger(MyDbContext dbContext, IHttpContextAccessor httpContextAccessor)
    {
        _dbContext = dbContext;
        _httpContextAccessor = httpContextAccessor;
    }

    public void LogRequest()
    {
        string bodyAsString = null;
        if (_httpContextAccessor.HttpContext.Request.Body.CanSeek)
        {
            RewindStream();

            using StreamReader reader = new StreamReader(_httpContextAccessor.HttpContext.Request.Body, Encoding.UTF8, true, 1024, true);
            bodyAsString = reader.ReadToEnd();

            RewindStream();
        }
            
        var uri = _httpContextAccessor.HttpContext.Request.GetDisplayUrl();

        _dbContext.RequestMessages.Add(new RequestMessage { Message = bodyAsString, Received = DateTime.Now, Uri = uri });

        _dbContext.SaveChanges();
    }

    private void RewindStream() => _httpContextAccessor.HttpContext.Request.Body.Position = 0;
}

只需将它注入您的控制器并像这样使用它;

[HttpGet("hello")]
public ActionResult Hello()
{
    _requestMessageLogger.LogRequest();
    return Ok();
}

【讨论】:

    【解决方案2】:

    这就是我要找的。我用了一个中间件。

    using Issue2.Model;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Http;
    using System.Threading.Tasks;
    
    namespace Issue2.Middleware
    {
        // You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
        public class Middleware1
        {
            private readonly RequestDelegate _next;
    
            public Middleware1(RequestDelegate next)
            {
                _next = next;
            }
    
            public Task Invoke(HttpContext httpContext, AppDbContext appDbContext)
            {
                var status = httpContext.Response.StatusCode;
                var url = httpContext.Request.Host.ToString();
                var method = httpContext.Request.Method.ToString();
                appDbContext.requestMessages.Add(new RequestMessage { Received = DateTime.Now, Method = method, Url = url, Status = status });
                appDbContext.SaveChanges();
                return _next(httpContext);
            }
        }
    
        // Extension method used to add the middleware to the HTTP request pipeline.
        public static class Middleware1Extensions
        {
            public static IApplicationBuilder UseMiddleware1(this IApplicationBuilder builder)
            {
                return builder.UseMiddleware<Middleware1>();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多