NLog: NLog配置文件在哪里?

3

我写了这个代码:

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...");
}

什么都没有显示出来... 所以我被告知要在配置文件中添加<targets>,问题是... 配置文件在哪里?Google上没有任何帮助,文档也没有。


1
使用Nuget添加NLog。它将向您的项目添加配置文件。 - Shyju
据我所见,我没有NLog.config文件,只有一个引用中的.DLL。 - Ashkru
你需要使用Nuget 3+创建自己的nlog.config。 - Julian
3个回答

6

来自NLog维基:

执行独立的*.exe应用程序时,将搜索以下位置:

  • 标准应用程序配置文件(通常为applicationname.exe.config)
  • 应用程序目录中的applicationname.exe.nlog
  • 应用程序目录中的NLog.config
  • NLog.dll所在目录中的NLog.dll.nlog(仅当NLog未安装在GAC中时)

因此,在应用程序目录中添加一个NLog.config文件是最简单的方法。


2

2

以下是供您参考的示例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"
      throwConfigExceptions="true">

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

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

另请参阅:https://github.com/NLog/NLog/wiki/Tutorial


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接