为什么不是nlog保存一个糟糕的请求网址?

时间:2017-05-11 18:21:09

标签: asp.net-mvc asp.net-core nlog

我使用ASP.NET Core和NLog.Web.AspNetCore(4.3.1)。 NLog没有保存错误的请求网址 - 为什么? 那是我的NLog.config:

<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <targets>
    <target name="logfile" xsi:type="File" fileName="${basedir}/nlog.txt"
            layout="${longdate} url: ${aspnet-request-url} | ${message}"/>
  </targets>

  <rules>
    <logger name="*" minlevel="Warn" writeTo="logfile"/>
  </rules>
</nlog>

当我遇到404错误时,nlog正在保存:

  

2017-05-11 20:07:34.2466 url:|我的错误消息

以上网址为空 - 为什么?

我在Startup.cs中的配置方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            // .....

            app.UseExceptionHandler("/Error/ApplicationError");
            app.UseStatusCodePagesWithReExecute("/Error/Error/{0}");

            // .....

            loggerFactory.AddNLog();
            app.AddNLogWeb();

            // ......
        }

我的错误控制器:

public class ErrorController : Controller
{
    private readonly ILogger<ErrorController> _logger;

    public ErrorController(ILogger<ErrorController> logger)
    {
        _logger = logger;
    }

    public IActionResult ApplicationError()
    {
        return View();
    }

    [Route("/Error/Error/{statusCode}")]
    public IActionResult Error(int statusCode)
    {
        _logger.LogError("My error message");

        return View(statusCode);
    }
}

1 个答案:

答案 0 :(得分:3)

尝试将AspnetCore扩展添加到NLog配置中:

<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
    </extensions>

    ...
</nlog>

有关详细信息,请参阅the NLog AspnetCore documentation