为什么 nlog 不保存一个错误的请求 url?
why isn't nlog saving a bad request url?
我使用 ASP.NET Core 和 NLog.Web.AspNetCore (4.3.1)。 NLog 没有保存错误的请求 url - 为什么?
那是我的 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: | My error message
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);
}
}
尝试将 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。
我使用 ASP.NET Core 和 NLog.Web.AspNetCore (4.3.1)。 NLog 没有保存错误的请求 url - 为什么? 那是我的 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: | My error message
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);
}
}
尝试将 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。