需要登录asp.net的WebAPI 2请求和响应体到数据库 [英] Need to log asp.net webapi 2 request and response body to a database

查看:208
本文介绍了需要登录asp.net的WebAPI 2请求和响应体到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用微软Asp.net WebApi2 IIS上承载的。我很干脆想记录的请求体(XML或JSON)和每个岗位的响应体。

有什么特别的项目或控制器处理后。我没有兴趣使用日志​​框架,如n日志,ELMAH,log4net的,或内置在追查的WebAPI的特征,除非有必要这么做。

我只是想知道在哪里把我的记录code和如何从传入和传出请求和响应的实际JSON或XML。

我的控制器POST方法:

 公开的Htt presponseMessage邮报([FromBody])员工员工)
{
   如果(ModelState.IsValid)
   {
      //插入到员工数据库
   }}


解决方案

我会建议使用一个DelegatingHandler。然后,你将不必担心在控制器任何日志记录code。

 公共类LogRequestAndResponseHandler:DelegatingHandler
{
    保护覆盖异步任务< Htt的presponseMessage> SendAsync(
        HTT prequestMessage请求的CancellationToken的CancellationToken)
    {
        //登录请求主体
        字符串requestBody =等待request.Content.ReadAsStringAsync();
        Trace.WriteLine(requestBody);        //让其他处理程序处理请求
        VAR的结果=等待base.SendAsync(请求的CancellationToken);        如果(result.Content!= NULL)
        {
            //一旦响应体准备好了,它记录
            VAR responseBody =等待result.Content.ReadAsStringAsync();
            Trace.WriteLine(responseBody);
        }        返回结果;
    }
}

只是你的日志code替换Trace.WriteLine并注册在WebApiConfig处理程序是这样的:

config.MessageHandlers.Add(新LogRequestAndResponseHandler());

I am using Microsoft Asp.net WebApi2 hosted on IIS. I very simply would like to log the request body (xml or json) and the response body for each post.

There is nothing special about this project or the controller processing the post. I am not interested in using logging frameworks like nLog, elmah, log4net, or the built in tracing features of webapi unless it is necessary to do so.

I am simply wanting to know where to put my logging code and how to get the actual json or xml from the incoming and outgoing request and response.

My controller post method:

public HttpResponseMessage Post([FromBody])Employee employee)
{
   if (ModelState.IsValid)
   {
      // insert employee into to database
   }

}

解决方案

I would recommend using a DelegatingHandler. Then you will not need to worry about any logging code in your controllers.

public class LogRequestAndResponseHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
    {
        // log request body
        string requestBody = await request.Content.ReadAsStringAsync();
        Trace.WriteLine(requestBody);

        // let other handlers process the request
        var result = await base.SendAsync(request, cancellationToken);

        if (result.Content != null)
        {
            // once response body is ready, log it
            var responseBody = await result.Content.ReadAsStringAsync();
            Trace.WriteLine(responseBody);
        }

        return result;
    }
}

Just replace Trace.WriteLine with your logging code and register the handler in WebApiConfig like this:

config.MessageHandlers.Add(new LogRequestAndResponseHandler());

这篇关于需要登录asp.net的WebAPI 2请求和响应体到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆