2012-09-19 62 views
2

你好,亲爱的开发商,NLOG拒绝,抛出异常

我遇到了一个奇怪的问题与NLOG,
我没有崩溃,但无法找到日志中的用户活动痕迹。

我认为,日志记录不工作...
尝试搜索权限问题等,但似乎都是O.K.

我想调试我的情况问题的记录,所以我创建了下面的
代码来告诉用户,如果它未能创造任何东西:

public static class Log 
{ 
    static Log() 
    { 
     try 
     { 
      AppLogger = LogManager.GetLogger("Megatec.EngineeringMatrix.AppLogger"); 
      ChangeLogger = LogManager.GetLogger("Megatec.EngineeringMatrix.ChangeLogger"); 
      DRCLogger = LogManager.GetLogger("Megatec.EngineeringMatrix.DRCLogger"); 

      if((AppLogger == null) || (ChangeLogger == null) || (DRCLogger == null)) 
       throw new NLogConfigurationException("Configuration does not specify correct loggers"); 
      writeStartupLogEntry(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString(), @"Failed to load `Megatec.EngineeringMatrix` Loggers."); 
      throw; 
     } 

    } 

    public static readonly Logger AppLogger; 
    public static readonly Logger ChangeLogger; 
    public static readonly Logger DRCLogger; 

与下面的配置文件:

<?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" throwExceptions="true"> 
    <targets> 
     <target xsi:type="File" name="AppLogFile" createDirs="true" fileName="c:/log?s/${processname}/${logger}_${shortdate}.log" 
       layout=">> ${time} ${uppercase:${level}} ${callsite} ${message} ${exception:format=tostring}" /> 

     <target xsi:type="File" name="ChangeLogFile" createDirs="true" fileName="c:/log?s/${processname}/${logger}_${shortdate}.log" 
       layout=">> ${date} ${message} ${exception:format=tostring}" /> 

     <target xsi:type="File" name="DRCLogFile" createDirs="true" fileName="c:/logs/${processname}/${logger}_${shortdate}.log" 
       layout=">> ${date} ${message} ${exception:format=tostring}" /> 
    </targets> 

    <rules> 
     <logger name="Megatec.EngineeringMatrix.AppLogger" minlevel="Trace" writeTo="AppLogFile" /> 
     <logger name="Megatec.EngineeringMatrix.ChangeLogger" minlevel="Trace" writeTo="ChangeLogFile" /> 
     <logger name="Megatec.EngineeringMatrix.DRCLogger" minlevel="Trace" writeTo="DRCLogFile" /> 
    </rules> 
    </nlog> 

明显我写了一个BAAD配置,因为无法创建目录c:\lo?gs

不过,我仍然没有得到消息
MessageBox.Show(ex.ToString(), @"Failed to load 'Megatec.EngineeringMatrix'

甚至AppLogger.Info()跳过例外...

什么我写的日志,但应用程序不知道它.. 。

在调试,我可以捕获异常,看看它是由NLOG处理,

我怎样才能抓住它从我的代码?

+0

您是否尝试过的故障排除步骤在这里:http://nlog-project.org/wiki/Configuration_file#Troubleshooting_logging?默认情况下,日志记录异常被吞噬。 – Joe

+1

是的,我添加了necessery配置参数,你可以看到.. 但仍然。我可以在internalLog中看到,但没有传递给应用程序。 –

回答

1

这是一个老问题,但最近有一些变化。

一些例外情况在过去被NLog'吃掉'。例如在AsyncWrapper(或使用属性async<target>

这已被固定在NLOG 4.3:

异常(行为CHANGE) 测井和例外的投掷的一致处理以前不一致。并非所有的数据都记录到内部记录器,有时甚至丢失。例如,异步包装器(或异步属性)捕捉所有异常而没有适当的重新抛出。

这很糟糕,因为它有时不清楚为什么NLog不工作(多次获得它自己)。这已在NLog 4.3中修复!

将所有异常记录到内部记录器 在任何情况下都将遵守“throwExceptions”选项 建议:在生产环境中禁用throwExceptions! (这个默认值)

看到http://nlog-project.org/2016/04/16/nlog-4-3-has-been-released.html