18

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?

1
  • 1
    Follow up: I used ${aspnet-request:serverVariable=SERVER_NAME} to get the domain
    – Mike737
    Sep 7, 2011 at 22:55

3 Answers 3

18

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.

4
8

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-...

1
  • Further to this. You will also need to have NLog.Web installed, too.
    – Lee Taylor
    Jun 27, 2020 at 15:53
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.