1
如何为NLog呈现的asp *布局应该工作?NLog记录请求信息异常
我有以下设置
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'派生你想实现什么? – nemesv 2014-11-01 08:56:45
我从ExceptionLogger派生,并将我的类注册为webapi堆栈中的一个服务,以获取请求管道中抛出的每个异常的通知,以便我可以通过NLog记录它们。我可以导致实现IExceptionLogger自己,但我发现重写一个方法更容易。 您认为这与问题有关吗? – Vegar 2014-11-01 21:32:48
http://www.asp.net/web-api/overview/releases/whats-new-in-aspnet-web-api-21#global-error – Vegar 2014-11-01 21:34:18