简体   繁体   English

未创建 Nlog 日志文件

[英]Nlog log file is not created

I am trying to log exceptions in console application.我正在尝试在控制台应用程序中记录异常。 I have done everything as always (and then it worked for me...):我像往常一样做了所有事情(然后它对我有用......):

NLog.config: 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"
 xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
 autoReload="true"
 throwExceptions="false"
 internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
  <targets>
    <target name="LogFile" xsi:type="File"
               fileName="${basedir}/Log/${date:format=yyyyMMdd} myLog.txt"
               layout="${longdate}|${level:uppercase=true}|${message}|${exception}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="LogFile" />
  </rules>
</nlog>
class Program
{
    private static Logger _log = LogManager.GetCurrentClassLogger();

    static void Main(string[] args)
    {
      _log.Error("test");
    }
}

For some mysterious reason file is never created nowhere on my computer.出于某种神秘的原因,我的计算机上从未创建过文件。 Any ideas why任何想法为什么

In Visual Studio, check if your NLog.config file has this properties: Build Action: Content.在 Visual Studio 中,检查您的 NLog.config 文件是否具有以下属性:构建操作:内容。 Copy to Output Directory = Copy if newer复制到输出目录 = 如果更新则复制

I am attaching a screen shot where you can see how it should appear.我附上了一个屏幕截图,您可以在其中看到它应该如何显示。 Sorry, it is in Spanish.对不起,它是西班牙语。

在此处输入图片说明

Your config looks valid, so that shouldn't be the issue. 您的配置看起来有效,因此不应该是问题。

Some things to check: 有些事要检查:

  1. Is you nlog.config in the correct directory? 你是否在正确的目录中使用nlog.config? The best way to test is to copy the nlog.config to the directory with your .dll or .exe. 测试的最佳方法是将nlog.config复制到包含.dll或.exe的目录。

  2. Enable exceptions to be sure that errors are not captured by Nlog: throwExceptions="true" 启用异常以确保Nlog不捕获错误:throwExceptions throwExceptions="true"

  3. Check the internal log by setting internalLogLevel="Info" and check "c:\\temp\\nlog-internal.log" 通过设置internalLogLevel="Info"检查内部日志,然后选中“c:\\ temp \\ nlog-internal.log”

A full tutorial to troubleshoot could be found on the NLog documentation . 可以在NLog文档中找到完整的故障排除教程。

Edit your NLog config, change your log file path like this,use ${shortdate} instead {date:format=yyyyMMdd} . 编辑NLog配置,像这样更改日志文件路径,使用${shortdate}代替{date:format=yyyyMMdd} Set name in rules and call it like in this sample. 在规则中设置名称并像在此示例中一样调用它。

 <?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 async="true">
    <targets>
      <target name="LogFile" xsi:type="File"
              fileName="${basedir}/Log/${shortdate}myLog.txt"
              layout="${longdate}|${level:uppercase=true}|${message}|${exception}" />
    </targets>

    <rules>
      <logger name="ErrorLog" minlevel="Trace" writeTo="LogFile" />
    </rules>
</nlog>

And your .cs 而你的.cs

    class Program
{

    private Logger ErrorLogger = NLog.LogManager.GetLogger("ErrorLog");

    static void Main(string[] args)
    {
        ErrorLogger.Error("test");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM