2012-03-28 87 views
0

我沒有運氣讓NLog工作。通過tutorial的工作,我看到了那裏的確切代碼。不能讓NLog工作

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using NLog; 

namespace NLog2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var c = new MyClass(); 
      c.MyMethod1(); 
     } 
    } 

    public class MyClass 
    { 
     private static Logger logger = LogManager.GetCurrentClassLogger(); 

     public void MyMethod1() 
     { 
      logger.Trace("Sample trace message"); 
      logger.Debug("Sample debug message"); 
      logger.Info("Sample informational message"); 
      logger.Warn("Sample warning message"); 
      logger.Error("Sample error message"); 
      logger.Fatal("Sample fatal error message"); 

      // alternatively you can call the Log() method 
      // and pass log level as the parameter. 
      logger.Log(LogLevel.Info, "Sample fatal error message"); 
     } 
    } 
} 

我的配置文件(名爲NLog.config)看起來像......

<?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"> 

    <targets> 
     <target name="logfile" xsi:type="File" fileName="file.txt" /> 
    </targets> 

    <rules> 
     <logger name="*" minlevel="Info" writeTo="logfile" /> 
    </rules> 
</nlog> 

我沒有得到任何輸出。有人可以看到這個問題在這裏嗎?

+0

在'nlog'元素上設置'throwExceptions =「true」'和'internalLogFile =「nlog.txt」internalLogLevel =「Trace」'。也許你會在內部的日誌文件中看到一些東西......是在bin目錄中的'NLog.config'嗎? – nemesv 2012-03-28 20:46:13

回答

3

愚蠢的錯誤。我沒有將配置文件設置爲複製到輸出目錄。

+0

我確信這是一個noob問題 - 如何讓Visual Studio在構建時自動將NLog.config複製到輸出文件夾? – 2012-08-29 13:00:27

+2

@WebUser:在解決方案資源管理器中選擇文件,然後確保'Build Action'設置爲'Content'並且'Copy to Output Directory'設置爲'Do not copy'以外的其他值 – DilbertDave 2012-09-02 10:22:36

+0

Thanks @DilbertDave ! – 2012-09-03 11:33:30

3

Web用戶,在解決方案資源管理器中的uour文件列表中找到您的NLog.config。右鍵單擊NLog.config並選擇屬性。在「複製到輸出目錄」處選擇「複製到輸出目錄」並且很開心^^