千家信息网

.NET 6中间件Http Logging怎么使用

发表于:2024-11-19 作者:千家信息网编辑
千家信息网最后更新 2024年11月19日,这篇文章主要介绍".NET 6中间件Http Logging怎么使用",在日常操作中,相信很多人在.NET 6中间件Http Logging怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的
千家信息网最后更新 2024年11月19日.NET 6中间件Http Logging怎么使用

这篇文章主要介绍".NET 6中间件Http Logging怎么使用",在日常操作中,相信很多人在.NET 6中间件Http Logging怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答".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.1info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]      Response:      StatusCode: 200      Content-Type: application/json; charset=utf-8info: 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: 15info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[3]      RequestBody: {"name":"test"}info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]      Response:      StatusCode: 200      Content-Type: application/json; charset=utf-8info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[4]      ResponseBody: {"name":"test"}

到此,关于".NET 6中间件Http Logging怎么使用"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0