2012-09-19 65 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