Martossy Alex
Martossy Alex

Reputation: 115

Any alternatives to aspnet-request:serverVariable when using NLog with .Net Core?

As stated on the NLog GitHub the ${aspnet-request:serverVariable=String} layout renderer is not supported in .Net Core.
The documentation doesn't provide alternatives to many of the variables available under serverVariable.

My question is, are there any alternatives? Like to access remote address, server name, port etc? Or do I just have to write a bunch of custom layout renderers documented here and dependency inject all the stuff by hand?

Upvotes: 1

Views: 1056

Answers (1)

Julian
Julian

Reputation: 36790

For ASP.NET Core there as many new layout renders. The reason is that the API of ASP.NET Core is very different and the server variables can't be read like in ASP.NET (so non-core)

There are currently 13 layout renders for ASP.NET Core that renders a part of the request.

  • ${aspnet-request} - ASP.NET Request variable.
  • ${aspnet-request-contenttype} - ASP.NET Content-Type header (Ex. application/json)
  • ${aspnet-request-cookie} - ASP.NET Request cookie content.
  • ${aspnet-request-form} - ASP.NET Request form content.
  • ${aspnet-request-headers} - ASP.NET Header key/value pairs.
  • ${aspnet-request-host} - ASP.NET Request host
  • ${aspnet-request-ip} - Client IP.
  • ${aspnet-request-method} - ASP.NET Request method (GET, POST etc).
  • ${aspnet-request-posted-body} - ASP.NET posted body / payload
  • ${aspnet-request-querystring} - ASP.NET Request querystring.
  • ${aspnet-request-referrer} - ASP.NET Request referrer.
  • ${aspnet-request-url} - ASP.NET Request URL.
  • ${aspnet-request-useragent} - ASP.NET Request useragent.

See also https://nlog-project.org/config/?tab=layout-renderers&search=package:nlog.web.aspnetcore

If you need something else, you could indeed create a custom renderer. If you need the http request you could use:

AspNetLayoutRendererBase.Register("aspnet-request-myrenderer", (logevent, httpcontext, config) => ... );

You need to reference the NLog.Web.AspNetCore package for that.

Upvotes: 3

Related Questions