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

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

Closed
ThomasArdal opened this issue Sep 17, 2019 · 13 comments
Closed

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

ThomasArdal opened this issue Sep 17, 2019 · 13 comments
Labels
Milestone

Comments

@ThomasArdal
Copy link
Contributor

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
Copy link
Member

Hi

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

@ThomasArdal
Copy link
Contributor Author

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
Copy link
Member

Does other aspnet layout renders work?

@ThomasArdal
Copy link
Contributor Author

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
Copy link
Member

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

@ThomasArdal
Copy link
Contributor Author

ThomasArdal commented Sep 17, 2019

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
Copy link
Member

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
Copy link
Member

I think it should be optional. Working on that.

fixed with #465

@ThomasArdal
Copy link
Contributor Author

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
Copy link
Contributor

@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
Copy link
Member

fixed in 4.9 :)

@tet-web-dev
Copy link

@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
Copy link
Contributor Author

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

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

4 participants