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."]}}

最新文章

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

热门文章

  1. 腾讯云大数据套件Hermes-MR索引插件使用总结
  2. ubuntu android studio kvm
  3. 【BZOJ1112】[POI2008]砖块Klo Treap
  4. 统计文件中单词的个数---Shell及python版
  5. Android获取文件的MD5值
  6. MySQL中EXPLAIN解释命令(转载)
  7. 第二次去苹果店维修MacBook
  8. SQL----&gt;mySQl查看和更改端口
  9. 关于Controller层返回JSON字符串
  10. 【Python】获取翻页之后的各页面中的属性值。