2012-03-28 89 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并选择属性。在“复制到输出目录”处选择“复制到输出目录”并且很开心^^