-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
${aspnet-request-posted-body} is always empty in asp.net core 3.1 for API controllers #556
Comments
This should be fixed with #549 But where is the logger call in your code? |
Here is my controller code: using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class LogTestsController : ControllerBase
{
private readonly ILogger<LogTestsController> _logger;
public LogTestsController(ILogger<LogTestsController> logger)
{
_logger = logger;
}
[HttpPost]
[Route("testlog")]
public async Task<ActionResult> TestLog([FromBody]string request)
{
try
{
throw new ApplicationException("Test log message");
}
catch (Exception ex)
{
_logger.LogError(ex, "Test error logging");
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
}
} |
Could you please try with https://ci.appveyor.com/api/buildjobs/c7sghdjljvcbb229/artifacts/src%2FNLog.Web.AspNetCore%2Fbin%2FAny%20CPU%2Frelease%2FNLog.Web.AspNetCore.4.9.2.1647-PR549.nupkg ? (no need for AllowSynchronousIO then) |
Still doesn't log the request body. |
funny, I tested it like this: [HttpPost]
public async Task<IActionResult> AddItem(ItemModel model)
{
_logger.LogInformation("Added item {@Model}", model);
try
{
await System.IO.File.ReadAllLinesAsync("c:/wrong.txt");
}
catch (Exception e)
{
_logger.LogError(e, "Error when added item {@Model}", model);
throw;
}
return View("Index");
} and got this:
See branch |
@vlmironoff could you please edit the example and see if you could reproduce this? (clone this repo, move to branch |
@304NotModified Could you test this temp workaround on your end?:
|
Testing now! |
Excellent, that works. Nice short way for adding middleware :) |
Configure is now: public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
// needed for ${aspnet-request-posted-body} with an API Controller. Must be before app.UseEndpoints
app.Use(async (context, next) => {
context.Request.EnableBuffering();
await next();
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
``` |
I think we should add this in the docs and release 4.9.3 with the async fix, agree @vlmironoff ? |
Totally agree. Thank you! |
Add another scenario about when logging requets body, give me an error in
version info
What make me confused is that other projects work well in my solution except this one. After checking dependency packages from nuget, I found the abnormal project has these differents
3.0? wtf? Someone forgot to upgrade to 3.1 after taste netcore3.0. [doge] To resolve this problem, you need to upgrade to netcoreapp3.1 and Microsoft.AspNetCore.Mvc.NewtonsoftJson3.1.0. |
@vlmironoff + @vincentshow + @ryuuc Seems that using But the request-body-stream is not available during the startup of the HttpRequest, and also not available during the completion of the HttpRequest. In these situations then the And if ASP.NET Core-Engine and NLog attempts to read the request-body-stream at the same time (one for processing and NLog for logging), then everything will fail (randomly). Thus using Would it make sense to create a wiki-page about how to inject the request-body into Would it make sense to remove |
For starters created PullRequest #754 that removes |
Thanks to @bakgerman now NLog.Web.AspNetCore v5.1 re-introduces ${aspnet-request-posted-body} with help from middleware: app.UseMiddleware<NLog.Web.NLogRequestPostedBodyMiddleware>(); It is no longer necessary to explicit call |
Type:
NLog.Web/NLog.Web.AspNetCore version:
Platform:
Current NLog config (xml or C#, if relevant)
What is the current result?
${aspnet-request-posted-body} logs nothing
What is the expected result?
Request body text/json to be logged
Did you check the Internal log?
Yes. The internal log does not have errors.
Are there any workarounds?
No.
Additional Info:
Programmatically configured IIS and Kestrel in Startup.cs to allow synchronous IO that was disabled by default. nLog was erroring out in internal log without access to sync io:
Tried via AsyncWrapper, but no luck.
The text was updated successfully, but these errors were encountered: