Nlog: ${aspnet-mvc-action} & ${aspnet-request-url} not filled (blank)

Created on 23 Aug 2018  路  9Comments  路  Source: NLog/NLog

NLog version: 4.6.0
Platform: .NET Core 2.1

Current NLog config

 <targets>
    <target xsi:type="File" name="webapi-log" fileName="${basedir}/logs/nlog-webapi-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
  </targets>
  <rules>
    <logger name="Microsoft.*" maxLevel="Info" final="true" />
    <logger name="*" minlevel="Trace" writeTo="webapi-log" />
  </rules>

Current result:

2018-08-23 16:36:48.6687||INFO|API.Controllers.AccountController|User was authenticated |url: |action: 

What is the expected result?

2018-08-23 16:36:48.6687||INFO|API.Controllers.AccountController|User was authenticated |url: https://localhost:44360/account/home |action:Home 

Internal log:

2018-08-23 16:38:32.3342 Info Message Template Auto Format enabled
2018-08-23 16:38:32.3434 Info Loading assembly: NLog.Web.AspNetCore
2018-08-23 16:38:32.4739 Info Adding target File Target[webapi-log]
2018-08-23 16:38:32.4831 Info Configured from an XML element in ...\bin\Debug\netcoreapp2.1\NLog.config...
2018-08-23 16:38:32.5025 Info Found 25 configuration items
2018-08-23 16:38:32.5346 Info Configuration initialized.
2018-08-23 16:38:32.5508 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.5.8.8046. Product version: 4.5.8.

Thanks.

question

All 9 comments

@jaydubal Tested on AspNetCore ver. 2.1 and got the following output (First from Controller-constructor, and second from Controller-Get-method)

2018-08-23 18:50:39.1420|1|DEBUG|AspNetCore21.Controllers.ValuesController|NLog injected into ValuesController |url: https://localhost/api/values|action: Get
2018-08-23 18:50:50.5790|1|DEBUG|AspNetCore21.Controllers.ValuesController|Get |url: https://localhost/api/values|action: Get

Guess you have to provide a complete non-working example, as NLog is working fine on my machine.

Please check if you NLog integration is correct, see

https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2#4-update-programcs

@304NotModified Thanks! it works now after changing.

public static void Main(string[] args)
{
  CreateWebHostBuilder(args).Build().Run();
}

to

public static void Main(string[] args)
{
  CreateWebHostBuilder(args).UseNLog().Build().Run();
}

Is it possible to have this code in "Startup.cs"? eg. services.AddNLog(); or app.UseNLog();

"Startup.cs"? eg. services.AddNLog(); or app.UseNLog();

AFAIK the startup.cs way is ASP.NET Core 1

clear, well currently that's not supported in NLog for ASP.NET Core 2

Maybe you can do this in Startup.cs (If not wanting to use UseNLog in Program.cs)

using Microsoft.Extensions.DependencyInjection;
using NLog.Extensions.Logging;
using NLog.Web;

public class Startup
{
  // This method gets called by the runtime. Use this method to add services to the container.
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddMvc();
    services.AddHttpContextAccessor();
  }

  // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  {
    app.ApplicationServices.SetupNLogServiceLocator();
    loggerFactory.AddNLog();
  }
}

Yes, this code works as well. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smeegoan picture smeegoan  路  3Comments

haythamabutair picture haythamabutair  路  3Comments

geedsen picture geedsen  路  3Comments

Jerefeny picture Jerefeny  路  3Comments

ErcinDedeoglu picture ErcinDedeoglu  路  3Comments