在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Intro.NET 6 会引入一个 Http logging 的中间件,可以用来帮助我们比较方便记录请求和响应的信息 Sample废话不多说,直接来看示例吧 var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); var app = builder.Build(); app.UseHttpLogging(); app.MapControllers(); app.Run();
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 默认地, 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 注意看一下请求头里的 接着我们来配置一下记录请求信息和响应信息,可以配置 builder.Services.AddHttpLogging(options => { options.LoggingFields = Microsoft.AspNetCore.HttpLogging.HttpLoggingFields.All; 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 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 执行 请求一下 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 的时候,日志级别记录为 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持极客世界。 |
请发表评论