.NET 6中间件Http Logging使用介绍
intro
.net 6 会引入一个 http logging 的中间件,可以用来帮助我们比较方便记录请求和响应的信息
sample
废话不多说,直接来看示例吧
var builder = webapplication.createbuilder(args); builder.services.addcontrollers(); var app = builder.build(); app.usehttplogging(); app.mapcontrollers(); app.run();
dotnet run
运行起来项目,然后访问一个接口就可以看到打印出来的 http logging 的日志了
info: microsoft.aspnetcore.httplogging.httploggingmiddleware[1] request: protocol: http/1.1 method: get scheme: http pathbase: path: /weatherforecast accept: 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.9 connection: keep-alive host: localhost:5084 user-agent: mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/94.0.4606.54 safari/537.36 accept-encoding: gzip, deflate, br accept-language: zh-cn,zh;q=0.9,en-us;q=0.8,en;q=0.7 cache-control: [redacted] upgrade-insecure-requests: [redacted] sec-ch-ua: [redacted] sec-ch-ua-mobile: [redacted] sec-ch-ua-platform: [redacted] sec-fetch-site: [redacted] sec-fetch-mode: [redacted] sec-fetch-user: [redacted] sec-fetch-dest: [redacted] info: microsoft.aspnetcore.httplogging.httploggingmiddleware[2] response: statuscode: 200 content-type: application/json; charset=utf-8 date: [redacted] server: [redacted] transfer-encoding: chunked
默认地,httploggingmiddleware
会记录请求的基本信息(请求地址,协议版本)和请求头信息以及响应状态和响应头信息,对于不在默认列表里的请求头和响应头,值会显示为 [redacted]
,如果需要记录这个请求头/响应头的值则需要配置 httploggingoptions
,可以在注册服务的时候进行配置,配置示例如下:
builder.services.addhttplogging(options => { options.requestheaders.add("cache-control"); options.responseheaders.add("server"); });
修改之后,重新启动并请求我们的服务,日志输出如下:
info: microsoft.aspnetcore.httplogging.httploggingmiddleware[1] request: protocol: http/1.1 method: get scheme: http pathbase: path: /weatherforecast accept: 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.9 connection: keep-alive host: localhost:5084 user-agent: mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/94.0.4606.54 safari/537.36 accept-encoding: gzip, deflate, br accept-language: zh-cn,zh;q=0.9,en-us;q=0.8,en;q=0.7 cache-control: max-age=0 upgrade-insecure-requests: [redacted] sec-ch-ua: [redacted] sec-ch-ua-mobile: [redacted] sec-ch-ua-platform: [redacted] sec-fetch-site: [redacted] sec-fetch-mode: [redacted] sec-fetch-user: [redacted] sec-fetch-dest: [redacted] info: microsoft.aspnetcore.httplogging.httploggingmiddleware[2] response: statuscode: 200 content-type: application/json; charset=utf-8 date: [redacted] server: kestrel transfer-encoding: chunked
注意看一下请求头里的 cache-control
和响应头里的 server
,原来都是 [redacted]
,配置之后就显示正确的值了,如果你要记录自定义的请求头信息,也是类似的配置
接着我们来配置一下记录请求信息和响应信息,可以配置 httploggingoptions
中的 loggingfields
来指定需要记录哪些信息
builder.services.addhttplogging(options => { options.loggingfields = microsoft.aspnetcore.httplogging.httploggingfields.all; options.requestheaders.add("cache-control"); options.responseheaders.add("server"); });
在上面的基础上增加 loggingfields
的配置,这里直接配置上所有的信息,此时再来重新请求,查看日志如下:
info: microsoft.aspnetcore.httplogging.httploggingmiddleware[1] request: protocol: http/1.1 method: get scheme: http pathbase: path: /weatherforecast host: localhost:5084 user-agent: dotnet-httpie/0.1.1 info: microsoft.aspnetcore.httplogging.httploggingmiddleware[2] response: statuscode: 200 content-type: application/json; charset=utf-8 info: microsoft.aspnetcore.httplogging.httploggingmiddleware[4] responsebody: [{"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"}]
可以看到此时的 response body 也记录下来了
我们再来增加一个 post 的 api 来验证一下 requestbody 是不是可以正常记录
[httppost] public iactionresult post(system.text.json.jsonelement element) => ok(element);
使用 dotnet-httpie 执行 http :5084/weatherforecast name=test
请求一下 api,输出日志如下:
info: microsoft.aspnetcore.httplogging.httploggingmiddleware[1] request: protocol: http/1.1 method: post scheme: http pathbase: path: /weatherforecast host: localhost:5084 user-agent: dotnet-httpie/0.1.1 content-type: application/json; charset=utf-8 content-length: 15 info: microsoft.aspnetcore.httplogging.httploggingmiddleware[3] requestbody: {"name":"test"} info: microsoft.aspnetcore.httplogging.httploggingmiddleware[2] response: statuscode: 200 content-type: application/json; charset=utf-8 info: microsoft.aspnetcore.httplogging.httploggingmiddleware[4] responsebody: {"name":"test"}
more
仔细看上面的示例的话会发现一个问题,当要记录 responsebody 的时候,response header 的信息没有被完全记录下来,感觉像是一个 bug,提了一个 issue 还没回复,感兴趣的可以参考:<https://github.com/dotnet/aspnetcore/issues/36920>
另外感觉这个中间件的日志级别都是 information 级别的,如果可以根据响应状态来动态配置日志级别就好了,比如说响应状态码大于等于 500 的时候,日志级别记录为 error
, 这样就可以有效地去除很多不必要的日志了,提了一个简陋的 pr,有兴趣的可以参考:https://github.com/dotnet/aspnetcore/pull/36873
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。