Reputation: 846
I'm trying to log the current pages URL as part of our NLog layout. I've scoured the NLog layout variables and have come up with nothing.
Does anyone know how to get the current page URL as a variable for the layout? Also is it possible just to extract the domain name so this can be used in the EventLog source attribute?
Upvotes: 18
Views: 7807
Reputation: 6967
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
Upvotes: 5
Reputation: 3194
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-...
Upvotes: 8
Reputation: 10344
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.
Upvotes: 18