NLog / NLog.Web

NLog integration for ASP.NET & ASP.NET Core 2-8
https://nlog-project.org
BSD 3-Clause "New" or "Revised" License
320 stars 166 forks source link

Cannot get ${aspnet-request-cookie} to render any cookies #463

Closed ThomasArdal closed 5 years ago

ThomasArdal commented 5 years ago

I'm trying to extend my target with resolving of HTTP cookies using the NLog.Web and NLog.Web.AspNetCore` packages. I cannot seem to get NLog to render any cookies. Here is what I'm doing (highly simplified):

public class MyTarget : AsyncTaskTarget
{
    public Layout CookieLayout { get; set; } = "${aspnet-request-cookie:outputFormat=Json}";

    protected override Task WriteAsyncTask(IList<LogEventInfo> logEvents, CancellationToken cancellationToken)
    {
        var renderedJson = RenderLogEvent(CookieLayout, logEvent);
        ...
    }
}

I have created both an ASP.NET MVC and ASP.NET Core MVC project and tried to set up the target in both projects. When debugging through the code, the WriteAsyncTask is called, but the renderedJson string is empty in both cases.

I was thinking if this was caused by the target being implemented as a AsyncTaskTarget why I tried to port it back to TargetWithContext. But doing so still produces the empty string representing the cookies.

Can you help figuring out what is going wrong here?

304NotModified commented 5 years ago

Hi

Have you checked the internal log? I would expect some clues there.

ThomasArdal commented 5 years ago

Not much I'm afraid:

2019-09-17 11:17:00.1811 Debug Setting 'AspNetRequestCookieLayoutRenderer.outputFormat' to 'Json'
...
2019-09-17 11:17:03.0692 Debug Targets for Microsoft.AspNetCore.Mvc.ViewFeatures.CookieTempDataProvider by level:
2019-09-17 11:17:03.0871 Debug Trace =>
2019-09-17 11:17:03.0871 Debug Debug =>
2019-09-17 11:17:03.0871 Debug Info =>
2019-09-17 11:17:03.1021 Debug Warn => elmahio
2019-09-17 11:17:03.1021 Debug Error => elmahio
2019-09-17 11:17:03.1021 Debug Fatal => elmahio
...
2019-09-17 11:17:03.4949 Debug Targets for Microsoft.AspNetCore.CookiePolicy.CookiePolicyMiddleware by level:
2019-09-17 11:17:03.5069 Debug Trace =>
2019-09-17 11:17:03.5069 Debug Debug =>
2019-09-17 11:17:03.5069 Debug Info =>
2019-09-17 11:17:03.5069 Debug Warn => elmahio
2019-09-17 11:17:03.5328 Debug Error => elmahio
2019-09-17 11:17:03.5328 Debug Fatal => elmahio
304NotModified commented 5 years ago

Does other aspnet layout renders work?

ThomasArdal commented 5 years ago

It does. If I replace the cookie layout with ${aspnet-request-url} the rendered value contains the correct URL. Does it make any difference that I run this localhost? There are still cookies, but don't know if there could be a difference there?

304NotModified commented 5 years ago

I see that CookieNames is required. That could be the issue.

ThomasArdal commented 5 years ago

Oooh, you're right. Changing the layout to ${aspnet-request-cookie:cookieNames=.ASPXAUTH:outputFormat=Json} did include that cookie. I thought that this was optional because of the following in the documentation:

"A list of keys can be passed"

But I guess the can there reference the possibility of using a comma-separated list. Any chance of me getting through with including properties on those layouts to render everything?

304NotModified commented 5 years ago

But I guess the can there reference the possibility of using a comma-separated list.

Yes a list is supported.

thought that this was optional

I think it should be optional. Working on that.

304NotModified commented 5 years ago

I think it should be optional. Working on that.

fixed with https://github.com/NLog/NLog.Web/pull/465

ThomasArdal commented 5 years ago

fixed with #465

That is awesome! I wonder if this is the case for other of the layout renderers too? I've seen aspnet-request-querystring render correctly but there are other "array-based" layouts like aspnet-request-form.

Another thing. I noticed that there aren't a layout renderer for getting all server veriables (headers). Would you like me to do one or is that intentional? I also talked with Rolf about response layout renderers. Like a aspnet-response-statuscode and similar.

snakefoot commented 5 years ago

@ThomasArdal Would you like me to do one or is that intentional?

You are very welcome to create a PullRequest. There is already an issue to resolve: #355

304NotModified commented 5 years ago

fixed in 4.9 :)

tet-web-dev commented 4 years ago

@ThomasArdal I'm Having this same issue using NLog.Web.AspNetCore 4.9 with a Core 2.2 application. It is logging a blank string to the database field.

Which version of ASP.NET Core were you experiencing this issue with?

Its not a big deal to get this the old fashioned way but since the library has the functionality I might as well implement it.

        <commandText>
          insert into nlog.log (
          ReqUrl,ReqCookies
          ) values (
           @ReqUrl, @ReqCookies
          );
        </commandText>

        <parameter name="@ReqUrl" layout="${aspnet-request-url}" />
        <parameter name="@ReqCookies" layout="${aspnet-request-cookie}" />
ThomasArdal commented 4 years ago

Pretty sure it was ASP.NET Core 2.1. I believe it was when creating this sample that I first experienced the problem: https://github.com/elmahio/elmah.io.nlog/tree/master/samples/Elmah.Io.NLog.AspNetCore21