Skip to content
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} could throw 'Synchronous operations are disallowed' for ASP.NET Core 3 #548

Closed
304NotModified opened this issue Apr 19, 2020 · 6 comments · Fixed by #549
Labels
ASP.NET Core ASP.NET Core - all versions

Comments

@304NotModified
Copy link
Member

304NotModified commented Apr 19, 2020

NLog 4.7.0
Nlog.Target.Http 1.0.1
Nlog.web.AspNet.Core 4.9.1
Aspnet.Core 3.1.201

internal logging first was indicating :

Warn Exception in 'NLog.Web.LayoutRenderers.AspNetRequestPostedBody.Append()' 
Exception: System.InvalidOperationException: 
Synchronous operations are disallowed. 
Call ReadAsync or set AllowSynchronousIO to true instead.

After googling I found that I must add

 services.Configure<IISServerOptions>(options =>
 {
     options.AllowSynchronousIO = true;
  });

Originally posted by @husseinshaib1 in NLog/NLog#3906 (comment)

@304NotModified
Copy link
Member Author

304NotModified commented Apr 19, 2020

@304NotModified 304NotModified changed the title ${aspnet-request-posted-body} could throw a Synchronous operations are disallowed ${aspnet-request-posted-body} could throw 'Synchronous operations are disallowed' for ASP.NET Core 3 Apr 19, 2020
@vlmironoff
Copy link

For Kestrel use:
services.Configure<KestrelServerOptions>(options => { options.AllowSynchronousIO = true; });

@vlmironoff
Copy link

@304NotModified ${aspnet-request-posted-body} is always empty in logs. I'm using asp.net core 3.1
NLog.Web.AspNetCore assembly is added to nlog.config

@304NotModified
Copy link
Member Author

@vlmironoff I think it depends when you're doing the logger call and if it's async. Please open a new issue for ths

@diegosps
Copy link

diegosps commented Jun 8, 2020

@vlmironoff
This might help you or someone.
Like 304 said, if you are doing the logging after the request, you hava to enable the rewind of the stream:

public async Task Invoke(HttpContext httpContext, ILogger<RequestLogMiddleware> logger)
        {
            var sw = Stopwatch.StartNew();
            httpContext.Request.EnableBuffering(); //this line is the key (asp.net core 3.1)
            await _next(httpContext);

            sw.Stop();
            var code = httpContext.Response?.StatusCode;
            string usuario = string.Empty;
            if (!string.IsNullOrEmpty(httpContext.User.Identity.Name))
                usuario = httpContext.User.Identity.Name;
            if (httpContext.Request?.Method != "OPTIONS")
                logger.LogInformation("Testando o middleware: {usuario}, {codigo}, {tempo}", usuario, code, (int)Math.Truncate(sw.Elapsed.TotalMilliseconds));
        }

I also had to add the options.AllowSynchronousIO = true for kestrel and IIS

@snakefoot
Copy link
Contributor

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 context.Request.EnableBuffering(); as it is handled by the middleware.

@snakefoot snakefoot added ASP.NET Core ASP.NET Core - all versions and removed ASP.NET Core 3 labels Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ASP.NET Core ASP.NET Core - all versions
Projects
None yet
4 participants