.net 6 会引入一个 http logging 的中间件,可以用来帮助我们比较方便记录请求和响应的信息
废话不多说,直接来看示例吧
varbuilder=webapplication.createbuilder(args);builder.rvices.addcontrollers();varapp=builder.build();app.uhttplogging();app.mapcontrollers();app.run();
dotnet run
运行起来项目,然后访问一个接口就可以看到打印出来的 http logging 的日志了
info:microsoft.aspnetcore.httplogging.httploggingmiddleware[1]request:protocol:http/1.1method:getscheme:httppathba:path:/weatherforecastaccept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9connection:keep-alivehost:localhost:5084ur-agent:mozilla/5.0(windowsnt10.0;win64;x64)applewebkit/537.36(khtml,likegecko)chrome/94.0.4606.54safari/537.36accept-encoding:gzip,deflate,braccept-language:zh-cn,zh;q=0.9,en-us;q=0.8,en;q=0.7cache-control:[redacted]upgrade-incure-requests:[redacted]c-ch-ua:[redacted]c-ch-ua-mobile:[redacted]c-ch-ua-platform:[redacted]c-fetch-site:[redacted]c-fetch-mode:[redacted]c-fetch-ur:[redacted]c-fetch-dest:[redacted]info:microsoft.aspnetcore.httplogging.httploggingmiddleware[2]respon:statuscode:200content-type:application/json;chart=utf-8date:[redacted]rver:[redacted]transfer-encoding:chunked
默认地,httploggingmiddleware
会记录请求的基本信息(请求地址,协议版本)和请求头信息以及响应状态和响应头信息,对于不在默认列表里的请求头和响应头,值会显示为 [redacted]
,如果需要记录这个请求头/响应头的值则需要配置 httploggingoptions
,可以在注册服务的时候进行配置,配置示例如下:
builder.rvices.addhttplogging(options=>{options.requestheaders.add("cache-control");options.responheaders.add("rver");});
修改之后,重新启动并请求我们的服务,日志输出如下:
info:microsoft.aspnetcore.httplogging.httploggingmiddleware[1]request:protocol:http/1.1method:getscheme:httppathba:path:/weatherforecastaccept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9connection:keep-alivehost:localhost:5物各有短长084ur-agent:mozilla/5.0(windowsnt10.0;win64;x64)applewebkit/537.36(khtml,likegecko)chrome/94.0.4606.54safari/537.36accept-encoding:gzip,deflate,braccept-language:zh-cn,zh;q=0.9,en-us;q=0.8,en;q=0.7cache-control:max-age=0upgrade-incure-requests:[redacted]c-ch-ua:[redacted]c-ch-ua-mobile:[redacted]c-ch-ua-platform:[r等差数列定义edacted]c-fetch-site:[redacted]c-fetch-mode:[redacted]c-fetch-ur:[redacted]c-fetch-dest:[redacted]info:microsoft.aspnetcore.httplogging.httploggingmiddleware[2]respon:statuscode:200content-type:application/json;chart=utf-8date:[redacted]rver:kestreltransfer-encoding:chunked
注意看一下请求头里的 cache-control
和响应头里的 rver
,原来都是 [redacted]
,配置之后就显示正确的值了,如果你要记录自定糖蛋白的作用义的请求头信息,也是类似的配置
接着我们来配置一下记录请求信息和响应信息,可以配置 httploggingoptions
中的 loggingfields
来指定需要记录哪些信息
builder.rvices.addhttplogging(options=>{options.loggingfields=microsoft.aspnetcore.httplogging.httploggingfields.all;options.requestheaders.add("cache-control");options.responheaders.add("rver");});
在上面的基础上增加 loggingfields
的配置,这里直接配置上所有的信息,此时再来重新请求,查看日志如下:
info:microsoft.aspnetcore.httplogging.httploggingmiddleware[1]request:protocol:http/1.1method:getscheme:httppathba:path:/weatherforecasthost:localhost:5084ur-agent:dotnet-httpie/0.1.1info:microsoft.aspnetcore.httplogging.httploggingmiddleware[2]respon:statuscode:200content-type:application/json;chart=utf-8info:microsoft.aspnetcore.httplogging.httploggingmiddleware[4]responbody:[{"date":"2021-09-25t23:40:11.0164783+08:00","temperaturec":37,"temperaturef":98,"summary":"cool"},{"date"三角函数公式表:"2021-09-26t23:40:11.0164836+08:00","temperaturec":50,"temperaturef":121,"summary":"warm"},{"date":"2021-09-27t23:40:11.0164838+08:00","temperaturec":-7,"temperaturef":20,"summary":"scorching"},{"date":"2021-09-28t23:40:11.016484+08:00","temperaturec":39,"temperaturef":102,"summary":"freezing"},{"date":"2021-09-29t23:40:11.0164842+08:00","temperaturec":4,"temperaturef":39,"summary":"balmy"}]
可以看到此时的 respon body 也记录下来了
我们再来增加一个 post 的 api 来验证一下 requestbody 是不是可以正常记录
[httppost]publiciactionresultpost(system.text.json.jsonelementelement)=>ok(element);
使用 dotnet-httpie 执行 http :5084/weatherforecast name=test
请求一下 api,输出日志如下:
info:microsoft.aspnetcore.httplogging.httploggingmiddleware[1]request:protocol:http/1.1method:postscheme:httppathba:path:/weatherforecasthost:localhost:5084ur-agent:dotnet-httpie/0.1.1content-type:application/json;chart=utf-8content-length:15info:microsoft.aspnetcore.httploggi高三冲刺培训班ng.httploggingmiddleware[3]requestbody:{"name":"test"}info:microsoft.aspnetcore.httplogging.httploggingmiddleware[2]respon:statuscode:200content-type:application/json;chart=utf-8info:microsoft.aspnetcore.httplogging.httploggingmiddleware[4]responbody:{"name":"test"}
仔细看上面的示例的话会发现一个问题,当要记录 responbody 的时候,respon header 的信息没有被完全记录下来,感觉像是一个 bug,提了一个 issue 还没回复,感兴趣的可以参考:<https://github.com/dotnet/aspnetcore/issues/36920>
另外感觉这个中间件的日志级别都是 information 级别的,如果可以根据响应状态来动态配置日志级别就好了,比如说响应状态码大于等于 500 的时候,日志级别记录为 error
, 这样就可以有效地去除很多不必要的日志了,提了一个简陋的 pr,有兴趣的可以参考:/d/file/titlepic/login
本文发布于:2023-04-04 09:31:26,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/5a8d45d5eb5da1254fad53f0938e7366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:.NET 6中间件Http Logging使用介绍.doc
本文 PDF 下载地址:.NET 6中间件Http Logging使用介绍.pdf
留言与评论(共有 0 条评论) |