0

I try to plug NLog to my project, and do it for the first time, code looks like this:

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:

<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? Is it good practice to create many log files while running one application?

Shay
  • 131
  • 5
  • What are the filenames that it creates for the logs? – willaien Jun 25 '15 at 20:18
  • @willaien _logdataHH-mm-ss.log_ - So time is different in each log file corresponding to the time whem log file was created – Shay Jun 25 '15 at 20:23
  • Why are you creating logs with the current time in the filename? – willaien Jun 25 '15 at 20:26
  • To distinct them, after many runs it's going to be many log files and I can see where the first and last etc It's just convenient. Change it to constant file name, and now it logs only in one file – Shay Jun 25 '15 at 20:32
  • You might try this: http://stackoverflow.com/questions/5565597/delete-log-files-after-x-days/6932391#6932391 – willaien Jun 25 '15 at 20:52
  • It is better to have the name of the log file always the same. Think about reading the current log file. You may want to use software like BareTail. But it will be less effective if the current log file occasionally changes its name. So, keep the name of the current log file constant, but introduce [archiving](https://github.com/NLog/NLog/wiki/File-target) (search for word archive, there are several options). – Dialecticus May 16 '22 at 18:04

2 Answers2

0

This is happening because of the target filename in your Nlog.config file. 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. 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 localDate = DateTime.Now;

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

fileTarget.FileName = baseDirectory + @"\logs\" + currentDateString + "-LOGS.txt";
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Ryan
  • 1
  • 1
  • Instead of modifying NLog configuration at runtime, then you can consider to use [process-startup-timestamp](https://stackoverflow.com/a/72277631/193178) – Rolf Kristensen May 17 '22 at 16:58
0

NLog will render a new filename every second when using this layout:

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.

See also: https://github.com/NLog/NLog/wiki/ProcessInfo-Layout-Renderer

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70