2017-02-26 79 views
3

一些奇怪的原因n日志不显示任何东西,当我写到控制台,在Program.cs中Main()方法我分配nlog.config:NLog不显示任何内容?

LogManager.Configuration = new XmlLoggingConfiguration("assets/packages/nlog/nlog.config"); 

这里是配置:

<nlog throwExceptions="true"> 
    <targets> 
    <target name="file" type="File" fileName="${basedir}/assets/logging/log.txt" /> 
    </targets> 
    <rules> 
    <logger name="*" minLevel="Info" writeTo="File" /> 
    </rules> 
</nlog> 

下面是一个简单类:

private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); 

public MyClass() 
{ 
    Console.Write("lol"); 
    Logger.Debug("Debug test..."); 
    Logger.Error("Debug test..."); 
    Logger.Fatal("Debug test..."); 
    Logger.Info("Debug test..."); 
    Logger.Trace("Debug test..."); 
    Logger.Warn("Debug test..."); 
} 

我知道正在调用的方法,因为我得到的“笑”,只是没有实际在控制台上的日志,但它确实写到/assets/logging/log.txt文件。

+0

你确认你的文件名的路径是正确的:

class MyClass { private static Logger Logger = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { Console.Write("lol"); Logger.Debug("Debug test..."); Logger.Error("Debug test..."); Logger.Fatal("Debug test..."); Logger.Info("Debug test..."); Logger.Trace("Debug test..."); Logger.Warn("Debug test..."); } } 

从log.txt的输出? –

+0

是的,如果不是,它会抛出异常。 – Ashkru

+0

请检查https://github.com/NLog/NLog/wiki/Internal-logging – Julian

回答

2

的BASEDIR是相对的.exe

在这种情况下,它很可能是在斌\调试\资产\日志\ log.txt的

2

您需要将数据发送到控制台为好。 添加另一个发送到控制台的目标,如下所示: Nlog Console target git hub example

而且我想你也应该把这个目标在ruleslogger(在writeTo随着File

4

为了看你有没有添加目标,并为它的规则在控制台上的日志输出像这样:

<?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" /> 
    <target name="console" xsi:type="Console" /> 
</targets> 

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

通过(使用上述配置文件)创建一个简单的Logger类的例子为我工作

class MyLoggerClass 
{ 
    public static Logger Logger = LogManager.GetCurrentClassLogger(); 
} 

class MyClass 
{ 
    static void Main(string[] args) 
    { 

     Console.Write("lol"); 
     MyLoggerClass.Logger.Debug("Debug test..."); 
     MyLoggerClass.Logger.Error("Debug test..."); 
     MyLoggerClass.Logger.Fatal("Debug test..."); 
     MyLoggerClass.Logger.Info("Debug test..."); 
     MyLoggerClass.Logger.Trace("Debug test..."); 
     MyLoggerClass.Logger.Warn("Debug test..."); 
    } 
} 

或者,你可以直接在课堂上使用的记录器这样的:

2017-02-26 16:13:44.8388|ERROR|NLogTest.Program|Debug test... 
2017-02-26 16:13:44.8856|FATAL|NLogTest.Program|Debug test... 
2017-02-26 16:13:44.8856|INFO|NLogTest.Program|Debug test... 
2017-02-26 16:13:44.8971|WARN|NLogTest.Program|Debug test...