2016-11-15 54 views
1

我有一個用於存儲某些日誌數據但未創建日誌文件的Outlook(加載項快速)的C#加載項即使對記錄器的調用不會失敗。我在Win 10環境中使用VS 2013。從Outlook加載項調用時,NLog不會寫入日誌文件

我NLog.Config文件(存儲在文件夾中OutlookAddin \ BIN \調試,在同一位置OutlookAddIn.dll.config)如下:

<?xml version="1.0" ?> 
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<targets> 
    <target name="file" xsi:type="File" 
     layout="${longdate} ${logger} ${message}" 
     fileName="${specialfolder:ApplicationData}\FindAlike\NewMails.txt" 
     keepFileOpen="false" 
     encoding="iso-8859-2" /> 
</targets> 

<rules> 
    <logger name="*" writeTo="file" /> 
</rules> 

代碼中加載項爲聲明:

public AddinModule() 
    { 
     Application.EnableVisualStyles(); 
     InitializeComponent(); 
     // Please add any initialization code to the AddinInitialize event handler 
    } 

    private ADXOutlookAppEvents adxOutlookEvents; 
    private DateTime LastReceivedDate = DateTime.Now; 
    private Timer mailCheckTimer; 
    public static RegistryKey SoftwareKey = Registry.CurrentUser.OpenSubKey("Software", true); 
    public static RegistryKey AppNameKey = SoftwareKey.CreateSubKey("FindAlike"); 
    public static Logger logger = LogManager.GetCurrentClassLogger(); 

和常規測試日誌文件寫的是:

public static void TestNLog() 
    { 
     try 
     { 
      NLog.LogManager.ThrowExceptions = true; 

      logger.Info("test1"); 
      logger.Warn("test2"); 
      logger.Error("test3"); 

      var fileTarget1 = (FileTarget)NLog.LogManager.Configuration.FindTargetByName("file"); 
      var logEventInfo = new LogEventInfo { TimeStamp = DateTime.Now }; 
      string fileName = fileTarget1.FileName.Render(logEventInfo); 
      if (!System.IO.File.Exists(fileName)) 
       throw new Exception("Log file does not exist."); 
     } 
     catch (Exception Ex) 
     { 
      MessageBox.Show(Ex.Message); 
     } 
    } 

當調用TestNLog時,雖然目標文件是正確的,但表明日誌文件不存在,但表明配置文件已被成功讀取。

相同的代碼在包含在可執行文件中時按預期工作。

+0

請註明作爲一個答案:) – Julian

回答

1

除了@ SimonKarvis的答覆,nlog.config的位置可難。這對於單元測試是一樣的。

我會建議:

  1. 創建從C#,例如在配置

    var target = new FileTarget 
    { 
         FileName = logfile, 
         ReplaceFileContentsOnEachWrite = true, 
         CreateDirs = createDirs 
    }; 
    var config = new LoggingConfiguration(); 
    
    config.AddTarget("logfile", target); 
    
    config.AddRuleForAllLevels(target); 
    
    LogManager.Configuration = config; 
    
  2. 或負載從一個字符串的配置:

    string configXml = "<nlog>...<nlog>"; 
    XmlDocument doc = new XmlDocument(); 
    doc.LoadXml(configXml); 
    var config = new XmlLoggingConfiguration(doc.DocumentElement, Environment.CurrentDirectory); 
    LogManager.Configuration = config; 
    
  3. 或者至少可是好景不長,發現nlog.config的正確路徑和「喂」它NLOG。

    var pathToNlogConfig = "c:\.."; 
    var config = new XmlLoggingConfiguration(pathToNlogConfig); 
    LogManager.Configuration = config; 
    
+0

試過的方法2株 - 成功登錄從Outlook插件,而不會複製Nlog.config。使用的代碼顯示在答案中 – SimonKravis

0

另一個StackOverflow問題(How to use NLog for a DLL)提示需要將NLog.config放在與調用加載項的可執行文件相同的目錄中。這解決了這個問題。但是,這會使分發變得非常困難,因爲Outlook可執行文件的位置會根據Outlook版本而有所不同,並且需要具有管理員權限才能將文件複製到其中。也許另一個記錄器不需要這個。

感謝來自@Julian建議將以下代碼編程指定的NLOG配置和外接成功運行:

using NLog; 
    using NLog.Config; 
    using NLog.Targets; 
    using System.Xml; 
... 

public static Logger logger = LogManager.GetCurrentClassLogger(); 

.... 
    public static void ConfigNLog() 
      { 

      string xml = @" 
      <nlog xmlns=""http://www.nlog-project.org/schemas/NLog.xsd"" 
       xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""> 
       <targets> 
        <target name=""file"" xsi:type=""File"" 
          layout=""${longdate} ${logger} ${message}"" 
          fileName=""${specialfolder:ApplicationData}\FindAlike\NewMails.txt"" 
          keepFileOpen=""false"" 
          encoding=""iso-8859-2"" /> 
       </targets> 
       <rules> 
        <logger name=""*"" writeTo=""file"" /> 
       </rules> 
      </nlog>"; 

      StringReader sr = new StringReader(xml); 
      XmlReader xr = XmlReader.Create(sr); 
      XmlLoggingConfiguration config = new XmlLoggingConfiguration(xr, null); 
      NLog.LogManager.Configuration = config; 
     }