记录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."]}}
最新文章
- Linux 查看进程和删除进程
- Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码)
- 初探ECMAScript6
- iTween基础之Look(使对象面朝指定位置)
- 练习PYTHON之EPOLL
- innerHeight与clientHeight、innerWidth与clientWidth、scrollLeft与pageXOffset等属性
- Summary Ranges leetcode
- 【技术干货】git常用命令
- bzoj 2436: [Noi2011]Noi嘉年华
- Zeroc Ice原理介绍
- 一个出色的表格(React实现__ES5语法)
- Oracle Instance and Database
- python_字符串的格式化输出
- vue 起步
- requests 请求几个接口 出现’您的账户在其它设备使用过,为保障安全,需重新登入才能在本设备使用‘
- UML类图学习笔记
- iOS应用管理(优化)
- Elasticseach的评分机制
- 20145302张薇《课程设计》数据恢复——WinHex实践
- prisma 集成 pipelinedb测试