开发者

Log current page url with NLog

I'm trying to log the current pages URL as part of our NLog la开发者_JAVA百科yout. 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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜