中的.NET 6 HttpLogging RequestBody在日志中始终为空



我使用HttpLogging来记录到达我的端点的请求。我想记录整个请求。我在Program.cs 中设置了HttpLogging

builder.Services.AddHttpLogging(logging =>
{
logging.LoggingFields = HttpLoggingFields.All;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpLogging();

并更改appsettings中的日志记录级别。开发.json

"Microsoft.AspNetCore": "Information"

然后,当我从Postman或cURL发送请求时,我可以看到所有需要的信息,但RequestBody是ALWAYS空的(靠近日志末尾(

info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
Request:
Protocol: HTTP/1.1
Method: POST
Scheme: https
PathBase:
Path: /WeatherForecast
Accept: */*
Connection: keep-alive
Host: localhost:7269
User-Agent: PostmanRuntime/7.29.0
Accept-Encoding: gzip, deflate, br
Content-Type: application/json
Content-Length: 60
Postman-Token: [Redacted]
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint 'WebApplication1.Controllers.WeatherForecastController.Post (WebApplication1)'
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
Route matched with {action = "Post", controller = "WeatherForecast"}. Executing controller action with signature System.Collections.Generic.IEnumerable`1[WebApplication1.WeatherForecast] Post() on controller WebApplication1.Controllers.WeatherForecastController (WebApplication1).
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
Executing ObjectResult, writing value of type 'WebApplication1.WeatherForecast[]'.
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
Response:
StatusCode: 200
Content-Type: application/json; charset=utf-8
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
Executed action WebApplication1.Controllers.WeatherForecastController.Post (WebApplication1) in 8.0904ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'WebApplication1.Controllers.WeatherForecastController.Post (WebApplication1)'
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[3]
RequestBody:   //<----------------------------------------------- ALWAYS EMPTY
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[4]
ResponseBody: [{"date":"2022-03-17T18:05:11.5799408+01:00","temperatureC":41,"temperatureF":105,"summary":"Bracing"}]
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/1.1 POST https://localhost:7269/WeatherForecast application/json 60 - 200 - application/json;+charset=utf-8 19.0485ms

然而,我知道主体在那里,因为当我试图读取控制器中的主体并在断点处停止时,我可以看到变量中加载的主体。

using var reader = new StreamReader(HttpContext.Request.Body);
var myBody = await reader.ReadToEndAsync(); // {"username": "josef","password": "MyPassword"}

有一个从PostMan生成的请求cURL示例。

curl --location --request POST 'https://localhost:7269/WeatherForecast' 
--header 'Content-Type: application/json' 
--data-raw '{
"username": "josef",
"password": "MyPassword"
}'

我不知道是不是有一步我错过了或者类似的事情。我尝试更改不同的日志级别、不同的请求类型、正文和正文类型。RequestBody在日志中始终为空。

编辑1

我刚刚发现,当我在控制器中保留上面的body读取器并从中读取时,它实际上在日志中记录了正确的RequestBody。有人能解释这种行为吗?我不明白为什么只有当我手动读取正文时,它才会记录。

我已经完成了这项工作(所有这些都与Serilog有关(。我在代码中添加了以下内容:

  1. 注册日志设置:

    services.AddHttpLogging(logging =>
    {
    logging.LoggingFields = HttpLoggingFields.All;
    logging.RequestHeaders.Add(HeaderNames.Accept);
    logging.RequestHeaders.Add(HeaderNames.ContentType);
    logging.RequestHeaders.Add(HeaderNames.ContentDisposition);
    logging.RequestHeaders.Add(HeaderNames.ContentEncoding);
    logging.RequestHeaders.Add(HeaderNames.ContentLength);
    logging.MediaTypeOptions.AddText("application/json");
    logging.RequestBodyLogLimit = 4096;
    logging.ResponseBodyLogLimit = 4096;
    });
    
  2. 包括一些appsettings.json:

"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Information",
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
}
},

可能只需要应用程序设置,尽管如果你想要一个记录的请求表单提交,你肯定需要编码logging.MediaTypeOptions.AddText("multipart/form-data");

最新更新