0

I have been trying to log server details using nlog. But seems like there is some errors in layout renderer.

I have tried these:

<parameter name="@serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />

<parameter name="@port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />

<parameter name="@serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />

<parameter name="@remoteAddress"  layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />

These are from official documentation but still these are not working.

Official Github project link

I have these in project.json file

  "NLog.Extensions.Logging": "1.0.0-rtm-beta1",
  "NLog.Web.AspNetCore": "4.3.0",
Liam
  • 27,717
  • 28
  • 128
  • 190
user3127109
  • 3,421
  • 8
  • 24
  • 33

1 Answers1

1

As stated in the docs, the serverVariable option for ${aspnet-request} isn't supported in ASP.NET Core

serverVariable - ServerVariables item to be rendered. See for possible options: msdn. Not supported in ASP.NET Core.

There isn't a Server Variables collection in ASP.NET Core (in contract with ASP.NET), so we cannot use it.

We have replaced several constructions with new renderers, see this overview

  • ${aspnet-request:serverVariable=SERVER_NAME} => Is this ${aspnet-Request-Host}?
  • ${aspnet-request:serverVariable=SERVER_PORT} => ${aspnet-request-url:IncludeHost=false:IncludePort=true}
  • ${aspnet-request:serverVariable=LOCAL_ADDR} => I think - ${aspnet-request-url}
  • ${aspnet-request:serverVariable=REMOTE_ADDR}=> don't think this one is supported.

Update: if you need the IP, see How to configure NLog to get IP address .NET Core

Community
  • 1
  • 1
Julian
  • 33,915
  • 22
  • 119
  • 174
  • Not of all of this is correct eg: ${aspnet-request-url:IncludeHost=false:IncludePort=true} will return this https://:{portId}/{Rest of called path} – Taras Kyryliuk Apr 07 '20 at 13:59