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} produces empty log #583

Closed
Tabgyn opened this issue Jun 19, 2020 · 8 comments
Closed

${aspnet-request-posted-body} produces empty log #583

Tabgyn opened this issue Jun 19, 2020 · 8 comments
Labels

Comments

@Tabgyn
Copy link

Tabgyn commented Jun 19, 2020

Type:

  • Bug

NLog version: 4.7.2

NLog.Web version: 4.9.2

Platform: .Net 4.5

Current NLog config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwConfigExceptions="true">
  <targets>
    <target name="logfile" xsi:type="File" fileName="file.txt" layout="${aspnet-request}"/>    
  </targets>

  <rules>
    <logger name="*" minlevel="Error" writeTo="logfile"/>
  </rules>
</nlog>

C# Controller

using NLog;
using System;
using System.Web.Mvc;

namespace NLogSample.Controllers
{
    public class HomeController : Controller
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";

            return View();
        }

        public ActionResult Exception(string value, string body)
        {
            try
            {
                if(string.IsNullOrEmpty(value))
                    throw new ArgumentNullException(nameof(value));
            }
            catch (Exception ex)
            {
                Logger.Error(ex, ex.Message);
                throw;
            }

            return RedirectToAction("Index");
        }
    }
}
  • What is the current result?
    ${aspnet-request} logs nothing

  • What is the expected result?
    Request to be logged

  • Did you checked the Internal log?
    Yes. The internal log does not have errors.

  • Please post full exception details (message, stacktrace, inner exceptions)

Value cannot be null.
Parameter name: value
   at NLogSample.Controllers.HomeController.Exception(String value, String body) in C:\Users\Tiago\source\repos\NLogSample\NLogSample\Controllers\HomeController.cs:line 30
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()

  • Are there any workarounds?
    No
@snakefoot
Copy link
Contributor

The documentation says that you need to specify what part of the AspNet-Request-HttpContext that you want to output (cookie, header, item, form, queryString):

https://github.com/NLog/NLog/wiki/AspNetRequest-layout-renderer

You will also notice that for ASP.NET Core, then it has been split into several layoutrenderers.

Maybe try this instead:

layout="${aspnet-request-url:includeQueryString=true}

See also: https://github.com/NLog/NLog/wiki/AspNetRequest-Url-Layout-Renderer

@Tabgyn
Copy link
Author

Tabgyn commented Jun 26, 2020

Thank you! But now I'm trying to use ${aspnet-request-posted-body} and it doesn't show anything".

@snakefoot
Copy link
Contributor

Sounds like a different issue. Maybe duplicate of #556

@Tabgyn
Copy link
Author

Tabgyn commented Jun 29, 2020

I saw this issue, trying to work on a workaround since in my case it is Framework 4.5 and not Core

@snakefoot
Copy link
Contributor

@Tabgyn Have you checked the NLog InternalLogger ? https://github.com/NLog/NLog/wiki/Logging-troubleshooting

@Tabgyn
Copy link
Author

Tabgyn commented Jun 29, 2020

Yes

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwConfigExceptions="true"
      internalLogFile="c:\log.txt" internalLogLevel="Trace">
...

I don't see errors
log.txt

@snakefoot
Copy link
Contributor

Guess you have to start debugging, and figure out why NLog fails to extract anything from InputStream:

private static Stream GetBodyStream(HttpRequest httpRequest)
{
#if !ASP_NET_CORE
var body = httpRequest.InputStream;
#else
var body = httpRequest.Body;
#endif
return body;
}

@Tabgyn Tabgyn closed this as completed Jun 29, 2020
@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 changed the title ${aspnet-request} empty log ${aspnet-request-posted-body} empty log Jul 25, 2022
@snakefoot snakefoot changed the title ${aspnet-request-posted-body} empty log ${aspnet-request-posted-body} produces empty log Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants