日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

.NET?6中間件Http?Logging使用介紹_實(shí)用技巧

作者:DotNet?NB ? 更新時(shí)間: 2022-03-22 編程語(yǔ)言

Intro

.NET 6 會(huì)引入一個(gè) Http logging 的中間件,可以用來(lái)幫助我們比較方便記錄請(qǐng)求和響應(yīng)的信息

Sample

廢話不多說(shuō),直接來(lái)看示例吧

var?builder?=?WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
var?app?=?builder.Build();

app.UseHttpLogging();
app.MapControllers();

app.Run();

dotnet run 運(yùn)行起來(lái)項(xiàng)目,然后訪問(wèn)一個(gè)接口就可以看到打印出來(lái)的 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

默認(rèn)地,HttpLoggingMiddleware 會(huì)記錄請(qǐng)求的基本信息(請(qǐng)求地址,協(xié)議版本)和請(qǐng)求頭信息以及響應(yīng)狀態(tài)和響應(yīng)頭信息,對(duì)于不在默認(rèn)列表里的請(qǐng)求頭和響應(yīng)頭,值會(huì)顯示為 [Redacted],如果需要記錄這個(gè)請(qǐng)求頭/響應(yīng)頭的值則需要配置 HttpLoggingOptions,可以在注冊(cè)服務(wù)的時(shí)候進(jìn)行配置,配置示例如下:

builder.Services.AddHttpLogging(options?=>
{
????options.RequestHeaders.Add("Cache-Control");
????options.ResponseHeaders.Add("Server");
});

修改之后,重新啟動(dòng)并請(qǐng)求我們的服務(wù),日志輸出如下:

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

注意看一下請(qǐng)求頭里的 Cache-Control 和響應(yīng)頭里的 Server,原來(lái)都是 [Redacted],配置之后就顯示正確的值了,如果你要記錄自定義的請(qǐng)求頭信息,也是類似的配置

接著我們來(lái)配置一下記錄請(qǐng)求信息和響應(yīng)信息,可以配置 HttpLoggingOptions 中的 LoggingFields 來(lái)指定需要記錄哪些信息

builder.Services.AddHttpLogging(options?=>
{
????options.LoggingFields?=?Microsoft.AspNetCore.HttpLogging.HttpLoggingFields.All;
????options.RequestHeaders.Add("Cache-Control");
????options.ResponseHeaders.Add("Server");
});

在上面的基礎(chǔ)上增加 LoggingFields 的配置,這里直接配置上所有的信息,此時(shí)再來(lái)重新請(qǐng)求,查看日志如下:

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

可以看到此時(shí)的 response body 也記錄下來(lái)了

我們?cè)賮?lái)增加一個(gè) POST 的 API 來(lái)驗(yàn)證一下 RequestBody 是不是可以正常記錄

[HttpPost]
public?IActionResult?Post(System.Text.Json.JsonElement?element)?=>?Ok(element);

使用 dotnet-httpie 執(zhí)行 http :5084/weatherforecast name=test

請(qǐng)求一下 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

仔細(xì)看上面的示例的話會(huì)發(fā)現(xiàn)一個(gè)問(wèn)題,當(dāng)要記錄 ResponseBody 的時(shí)候,Response header 的信息沒有被完全記錄下來(lái),感覺像是一個(gè) BUG,提了一個(gè) issue 還沒回復(fù),感興趣的可以參考:<https://github.com/dotnet/aspnetcore/issues/36920>

另外感覺這個(gè)中間件的日志級(jí)別都是 Information 級(jí)別的,如果可以根據(jù)響應(yīng)狀態(tài)來(lái)動(dòng)態(tài)配置日志級(jí)別就好了,比如說(shuō)響應(yīng)狀態(tài)碼大于等于 500 的時(shí)候,日志級(jí)別記錄為 ERROR, 這樣就可以有效地去除很多不必要的日志了,提了一個(gè)簡(jiǎn)陋的 PR,有興趣的可以參考:https://github.com/dotnet/aspnetcore/pull/36873

原文鏈接:https://jishuin.proginn.com/p/763bfbd6811e

欄目分類
最近更新