.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
會記錄請求的基本信息(請求地址,協(xié)議版本)和請求頭信息以及響應狀態(tài)和響應頭信息,對于不在默認列表里的請求頭和響應頭,值會顯示為 [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 執(zhí)行 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
仔細看上面的示例的話會發(fā)現(xiàn)一個問題,當要記錄 ResponseBody 的時候,Response header 的信息沒有被完全記錄下來,感覺像是一個 BUG,提了一個 issue 還沒回復,感興趣的可以參考:<https://github.com/dotnet/aspnetcore/issues/36920>
另外感覺這個中間件的日志級別都是 Information 級別的,如果可以根據(jù)響應狀態(tài)來動態(tài)配置日志級別就好了,比如說響應狀態(tài)碼大于等于 500 的時候,日志級別記錄為 ERROR
, 這樣就可以有效地去除很多不必要的日志了,提了一個簡陋的 PR,有興趣的可以參考:https://github.com/dotnet/aspnetcore/pull/36873
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ASP.NET Core Api網(wǎng)關Ocelot的使用初探
這篇文章主要介紹了ASP.NET Core Api網(wǎng)關Ocelot的使用初探,幫助大家更好的理解和學習使用.NET技術,感興趣的朋友可以了解下2021-03-03一個Asp.Net的顯示分頁方法 附加實體轉換和存儲過程 帶源碼下載
現(xiàn)在自己寫的webform都不用服務器控件了,所以自己仿照aspnetpager寫了一個精簡實用的返回分頁顯示的html方法,其他話不說了,直接上代碼2012-10-10Global.asax的Application_BeginRequest實現(xiàn)url重寫無后綴的代碼
本文為大家詳細介紹下利用Global.asax的Application_BeginRequest 實現(xiàn)url重寫其無后綴,具體核心代碼如下,有需求的朋友可以參考下,希望對大家有所幫助2013-08-08Visual Studio Debug實戰(zhàn)教程之斷點操作
眾所周知斷點對于Visual Studio調試過程是十分重要的,斷點的設置也是為了更好的進行調試。下面這篇文章主要給大家介紹了關于Visual Studio Debug實戰(zhàn)教程之斷點操作的相關資料,需要的朋友可以參考下2018-09-09asp.net 刪除項目文件/文件夾IIS重啟,Session丟失問題
最近在做一個項目,涉及到大量文件中轉(先上傳到本項目的某個文件夾中,在移動到FTP中),后面發(fā)現(xiàn)每次一刪除文件之后在做操作都會提示未登錄,剛開始以為是WebService Session丟失問題,后面發(fā)現(xiàn)緩存也更新了2011-12-12Repeater控件分別綁定數(shù)組和ArrayList實現(xiàn)思路
在后臺用DataSource綁上數(shù)據(jù)源(數(shù)組或ArrayList)在調用DataBind()方法,在前臺調用%# GetDataItem()%,感興趣的朋友可以了解下啊,望本文可以鞏固你的數(shù)據(jù)綁定知識2013-01-01