【发布时间】:2014-10-31 12:41:17
【问题描述】:
NLog 的 asp* 布局渲染应该如何工作?
我有以下设置
packages.config
<package id="NLog" version="3.1.0.0" targetFramework="net45" />
<package id="NLog.Extensions" version="1.0.1.0" targetFramework="net45" />
NLog.config
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="errorlog" xsi:type="File" layout="${longdate} [${level}, ${logger}] ${message} ${newline} ${exception:format=tostring} ${newline} ${asp-request:serverVariable=String:queryString=String:item=String:form=String}" fileName="c://temp/${shortdate}-errors.log" />
</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="errorlog"/>
</rules>
</nlog>
日志记录 - 带有自定义消息
public class FileExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
LogManager.GetLogger<ExceptionLogger>().Error("Woops!", context.ExceptionContext.Exception);
}
}
记录错误时,我得到了预期的异常信息,但${asp-request...} 部分呈现为空。
如果我将消息的请求对象提供给记录器,它会按预期呈现请求信息:
记录 - 消息请求
public class FileExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
LogManager.GetLogger<ExceptionLogger>().Error(context.ExceptionContext.Request, context.ExceptionContext.Exception);
}
}
NLog 不应该自己获取请求信息吗?
如果没有,我应该如何登录 mye Woops!-message?
【问题讨论】:
-
你为什么从
ExceptionLogger派生你想达到什么目的? -
我从 ExceptionLogger 派生,并将我的类注册为 webapi 堆栈中的服务,以便收到请求管道中抛出的每个异常的通知,以便我可以通过 NLog 记录它们。我当然可以自己实现 IExceptionLogger,但我发现覆盖一种方法更容易。你认为这和问题有关系吗?
标签: asp.net asp.net-web-api nlog