简体   繁体   English

NLog 创建多个日志文件而不是一个

[英]NLog creates several log files instead of one

I try to plug NLog to my project, and do it for the first time, code looks like this:我尝试将 NLog 插入我的项目,并且第一次这样做,代码如下所示:

static class Program
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();

private static void Main(string[] args)
{
    logger.Trace("Enter Main");

    MyClass.DoWork();

    logger.Trace("Exit Main");
}

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

    public static void DoWork()
    {
        logger.Trace("Enter DoWork");

        var mgc = new MyGreatClass();
        var task = mgc.RunAsync(...);

        logger.Trace("Exit DoWork");        
    }
}

class MyGreatClass
{
   private static readonly Logger logger = LogManager.GetCurrentClassLogger();

   async Task<bool> RunAsync()
   {
       logger.Trace("Log something");
       await DoSomethig();
   }
}

And Nlog.config file looks like this: Nlog.config文件如下所示:

<targets>
    <target name="file" xsi:type="File" fileName="${basedir}/logdata${date:format=HH-mm-ss}.log" 
      layout="${date:format=HH\:mm\:ss}|${message}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="file" />
  </rules>

But while logging it creates 3 different log files, how to make Nlog to create and log in one file only?但是在记录时它会创建 3 个不同的日志文件,如何让 Nlog 只创建和登录一个文件? Is it good practice to create many log files while running one application?在运行一个应用程序时创建多个日志文件是一种好习惯吗?

This is happening because of the target filename in your Nlog.config file.发生这种情况是因为Nlog.config文件中的目标文件名。 Every time a log message is being generated, it's creating a new log file using:每次生成日志消息时,它都会使用以下命令创建一个新的日志文件:

fileName="${basedir}/logdata${date:format=HH-mm-ss}.log"

The date:format... portion is a call to DateTime.Now . date:format...部分是对DateTime.Now的调用。 My code was doing the same thing.我的代码也在做同样的事情。 Once I set a variable of DateTime.Now at the beginning of my code, I then passed that variable into the Nlog.config setup as the name and only one log file was created.一旦我在代码的开头设置了一个DateTime.Now变量,然后我将该变量作为名称传递到Nlog.config设置中,并且只创建了一个日志文件。

DateTime localDate = DateTime.Now;

string currentDateString = localDate.ToString("yyyyMMdd-hhmmss"); 

fileTarget.FileName = baseDirectory + @"\logs\" + currentDateString + "-LOGS.txt";

NLog will render a new filename every second when using this layout:使用此布局时,NLog 将每秒渲染一个新文件名:

fileName="${basedir}/logdata${date:format=HH-mm-ss}.log" 

Instead consider doing this:而是考虑这样做:

fileName="${basedir}/logdata${processinfo:StartTime:format=HH-mm-ss:cached=true}.log" 

Then NLog will use the process-startup-timestamp, that will not change every second.然后 NLog 将使用 process-startup-timestamp,它不会每秒都改变。

See also: https://github.com/NLog/NLog/wiki/ProcessInfo-Layout-Renderer另请参阅: https ://github.com/NLog/NLog/wiki/ProcessInfo-Layout-Renderer

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

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