记录web api的request以及response(即写log)
https://www.cnblogs.com/felixnet/p/5689501.html
https://blog.csdn.net/Vblegend_2013/article/details/83446229
https://stackoverflow.com/questions/27176329/web-api-request-content-empty
https://forums.asp.net/t/2127541.aspx?Get+Http+Raw+Request+and+Response 这个里面提到了DelegatingHandler
https://www.infoworld.com/article/3211590/how-to-log-request-and-response-metadata-in-aspnet-web-api.html 这个里面提到了DelegatingHandler
https://github.com/aspnet/AspNetWebStack/issues/150
综合上面几个链接,日志应该使用delegating handler里面处理。(ActionFilter里面,http request的content已经被读取过了,需要像第一个链接里面一样,先重置回去,再读,比较麻烦)
string requestMessage = requestContent.ReadAsStringAsync().Result; //request raw
string responseMessage = response.ToString(); //response raw
自己写的版本
public class LogHandler : DelegatingHandler
{
private string _requestId;
private readonly string _newLine = Environment.NewLine; protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
_requestId = DateTime.Now.Ticks.ToString();
var requestBody = request.Content.ReadAsStringAsync().Result;
LogUtil.CreateLog(LogLevel.Message,
$"{_requestId}{_newLine}{request.Method.Method} {request.RequestUri.AbsoluteUri} HTTP/{request.Version}{_newLine}{requestBody}"); var response = await base.SendAsync(request, cancellationToken);
string responseBody = response.Content.ReadAsStringAsync().Result;
LogUtil.CreateLog(LogLevel.Message, $"{_requestId}{_newLine}HTTP/{response.Version} {(int)response.StatusCode} {response.StatusCode}{_newLine}{responseBody}{_newLine}{_newLine}");
return response;
}
}
有ReadAsAsync扩展方法,自动进行类型的转换
var apiRequest = request.Content.ReadAsAsync<ApiRequest<dynamic>>().Result;
OwinRequestScopeContext.Current.Items["CountryCode"] = apiRequest.Header.OpCo;
这个地方如果需要重复读取数据的话
https://stackoverflow.com/questions/26942514/multiple-calls-to-httpcontent-readasasync
If you want to read again and again, you would probably want to read as stream and seek to beginning every time you read the stream.
But then if you want to do what do you now but get the second read working, you can seek to the beginning of the stream, after the first read, like this.
await httpContent.LoadIntoBufferAsync();
var X = await httpContent.ReadAsAsync<T>(); Stream stream = await httpContent.ReadAsStreamAsync();
stream.Seek(, SeekOrigin.Begin); var Y = await httpContent.ReadAsAsync<Dictionary<string, object>>();
中间的stream不需要using,否则会遇到这种错误
HTTP/1.1 400 BadRequest
{"Message":"The request is invalid.","ModelState":{"request":["Stream was not readable."]}}
记录web api的request以及response(即写log)的更多相关文章
- WebApi官网学习记录---web api中的路由
如果一条路由匹配,WebAPI选择controller和action通过如下方式: 1.找到controller,将"controller"赋值给{controller}变量 2. ...
- .net core web api 获取request body的纯文本
本文代码 https://github.com/wuhaibo/readPlainTextDotNetCoreWepApi 总有些时候我们希望获得Request body 的纯文本 那么怎么做呢?很简 ...
- Web API使用记录系列(一)创建API项目与基本配置
本系列文章主要记录Web API使用过程中的一些个人总结,包括创建API项目.基础配置.ApiTestClient使用与HelpPage页面的优化.Owin与OAuth的使用等. 本节主要内容是API ...
- Dynamics CRM2016 Web API之通过实体的primary key查询记录
CRM2016启用了webapi 而弃用了odata,作为码农的我们又开始学习新东西了. 下面是一段简单的查询代码,通过systemuser的primary key来查询一条记录 Web API查询方 ...
- .Net中使用SendGrid Web Api发送邮件(附源码)
SendGrid是一个第三方的解决邮件发送服务的提供商,在国外使用的比较普遍.国内类似的服务是SendCloud.SendGrid提供的发送邮件方式主要是两种, 一种是SMTP API, 一种是Web ...
- ASP.NET Web Api返回对象类型为JSON还是XML
在Umbraco平台上开发过程中,我用WebApi返回JSON result给前端 前端使用React调用这个web api来获取JSON result 我写的web api方法是返回JSON 类型的 ...
- Asp.Net Web API VS Asp.Net MVC
http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-W ...
- 【ASP.NET Web API教程】2.1 创建支持CRUD操作的Web API
原文 [ASP.NET Web API教程]2.1 创建支持CRUD操作的Web API 2.1 Creating a Web API that Supports CRUD Operations2.1 ...
- 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】
Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...
随机推荐
- DOM操作的性能问题
造成DOM操作性能差的原因:1.DOM操作的实现和ECMAscript的实现是两个独立的部分,之间通过接口来完成相应的DOM操作. 2.实时查询文档得到HTML集合,重复执行查询操作.特别是lengt ...
- sencha touch 入门系列 (一)sencha touch 简介
参考链接:http://mobile.51cto.com/others-278381.htm Sencha touch 是基于JavaScript编写的Ajax框架ExtJS,将现有的ExtJS整合J ...
- String() 函数把对象的值转换为字符串。
var test1 = new Boolean(1);var test2 = new Boolean(0);var test3 = new Boolean(true);var test4 = new ...
- [Bootstrap] install Bootstrap framework in window 7 by npm
Install with npm You can also install Bootstrap using npm: $ npm install bootstrap require('bootstra ...
- PMP 笔记
项目: 为创造独特的产品.服务或结果而进行的临时性工作. 项目特征: 独特性:Unique.临时性:Temporary.渐进明细. 渐进明细:预算越来越精细.比如三峡工程中,预算从10亿级的误差到1亿 ...
- java后端技术栈
- reflect 机制
1: Class.forName的作用?为什么要用? 答:调用该访问返回一个以字符串指定类名的类的对象. 2: 通过反射,有几种方法可以实例化Class类对象? 3种,第一种:Class.forNam ...
- 170718、springboot编程之发送邮件
Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Boot中使用Jav ...
- rk3188 公板调试记录
打开ccache后编译android果然快很多. make rk3188_sdk_defconfig 触摸屏有问题,然后再吧input底下的touchscreen 屏蔽掉 james@jame ...
- MySQL 慢查询日志工具之pt-query-digest
1. 工具简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tc ...