2014-10-31 78 views
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?

+0

为什么你从'ExceptionLogger'派生你想实现什么? – nemesv 2014-11-01 08:56:45

+0

我从ExceptionLogger派生,并将我的类注册为webapi堆栈中的一个服务,以获取请求管道中抛出的每个异常的通知,以便我可以通过NLog记录它们。我可以导致实现IExceptionLogger自己,但我发现重写一个方法更容易。 您认为这与问题有关吗? – Vegar 2014-11-01 21:32:48

+0

http://www.asp.net/web-api/overview/releases/whats-new-in-aspnet-web-api-21#global-error – Vegar 2014-11-01 21:34:18

回答

1

因此,有事实证明有多个事情出错这里....

  1. ${asp-request.. }是ASP。 ${aspnet-request...}是asp.net
  2. 当从文档的复制粘贴,你应该用实际

    ${asp-request:serverVariable=SOME_ACTUAL_VARIABLE} 
    
  3. 替代数据类型时得到的NuGet一些推广,它有助于获得正确的选择。 即使NLog.Extensions包含有用的布局,一个我试图使用在NLog.Extended包...

    <package id="NLog.Extended" version="3.1.0.0" targetFramework="net45" /> 
    
  4. 它不是足以让NuGet包。你也得告诉n日志吧......

    <nlog> 
        <extensions> 
        <add assembly="NLog.Extended" /> 
        </extensions> 
        <targets.../> 
        <rules.../> 
    </nlog> 
    

不过就是这样......

其实我结束了使用${aspnet-request-summary}NLog.Extensions -package,虽然。这有点冗长,但现在没关系。