Log current page url with NLog
Answer for .NET Core as I tried the above before finding that the above does not work for .NET Core
THE FOLLOWING DO NOT WORK FOR .NET CORE
${aspnet-request:serverVariable=Url}
${aspnet-request:item=HTTP_URL}
Instead use:
${aspnet-request-url}
${aspnet-request-querystring}
e.g.
<target name="jsonFile" xsi:type="File" fileName="log.json" archiveNumbering="DateAndSequence" archiveAboveSize="5000000" maxArchiveFiles="10">
<layout xsi:type="JsonLayout">
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<attribute name="message" layout="${message}" />
<attribute name="exception" layout="${exception:format=ToString,StackTrace:maxInnerExceptionLevel=2}" />
<attribute name="machineName" layout="${machinename}" />
<attribute name="ip" layout="${aspnet-request-ip}" />
<attribute name="method" layout="${aspnet-request-method}" />
<attribute name="url" layout="${aspnet-request-url}" />
<attribute name="query" layout="${aspnet-request-querystring}" />
<attribute name="postedBody" layout="${aspnet-request-posted-body}" />
<attribute name="userAgent" layout="${aspnet-request-useragent}" />
</layout>
</target>
For more see: https://nlog-project.org/config/?tab=layout-renderers&search=package:nlog.web.aspnetcore
Use the Aspnet-request layout renderer.
<target xsi:type="Trace"
name="trace"
layout="${message} ${aspnet-request:serverVariable=Url}" />
You have full access to the request properties.
I tested many approached and only "item" seems to work in regards to http://msdn.microsoft.com/en-us/library/ms524602.aspx
method = ${aspnet-request:item=HTTP_METHOD}
url with query string = ${aspnet-request:item=HTTP_URL}
user agent / browser = ${aspnet-request:item=HTTP_USER_AGENT}
and so on...
UPDATE
Additionally to my answer above (I don't remember if I knew it back then or not) but I found somewhere (also don't remember where, was a while ago) that NLog.Extended.dll must be also referenced to use functionality relevant to ASP.NET logging ie. layouts with ${aspnet-...