richardsonwtr
richardsonwtr

Reputation: 158

Is Nlog not archiving all files?

I'm logging one file per day, and I want to move the old ones to a subfolder every month. I'm feeling there is something off here.

These are my current settings. For testing purposes, I changed it to archive every minute:

<target name="file" xsi:type="File"
            layout="${longdate} ${logger} ${message}${exception:format=ToString}"
            fileName="${basedir}/LOG/${date:format=yyyy-MM-dd}.log"
            archiveFileName="${basedir}/LOG/OLD_LOGS/{#}.log"
            archiveDateFormat="yyyy-MM-dd"
            archiveNumbering="Date"
            archiveEvery="Minute" 
      />

Consider the actual date for this test: May 29th 2020.

  1. If I run my app now, the file 2020-05-29.log will be created in the LOG folder.

    .
    ├── ...
    ├── LOG
    │   └── 2020-05-29.log
    └── ...

  1. If I run my app one minute later, the file above will be archived to the newly created subfolder OLD_LOGS and a new file will be created in the LOG folder.
   .
   ├── ...
   ├── LOG
   │   ├── 2020-05-29.log
   │   ├── OLD_LOGS
   │      └── 2020-05-29.log
   └── ...
  1. If I change my clock to the next day (May 30th 2020) a new file will be created 2020-05-30.log
   .
   ├── ...
   ├── LOG
   │   ├── 2020-05-29.log
   │   ├── 2020-05-30.log
   │   ├── OLD_LOGS
   │      └── 2020-05-29.log
   └── ...
  1. If I run my app one minute later, the 2020-05-30.log file will be archived
  .
  ├── ...
  ├── LOG
  │   ├── 2020-05-29.log
  │   ├── 2020-05-30.log
  │   ├── OLD_LOGS
  │      ├── 2020-05-29.log
  │      └── 2020-05-30.log
  └── ...

Shouldn't the LOG/2020-05-29.log file have been archived?

Upvotes: 1

Views: 1712

Answers (1)

Kishan Vaishnav
Kishan Vaishnav

Reputation: 2631

There is a problem in your configuration for per minute archival.

NLog starts overriding the old file because your file format is yyyy-MM-DD. The correct configuration for per minute achieving, add below code.

    <target name="kFile" xsi:type="File"
        layout="${longdate} ${logger} ${message}${exception:format=ToString}"
        fileName="${basedir}/LOG/${date:format=yyyy-MM-dd}.log"
        archiveFileName="${basedir}/LOG/OLD_LOGS/{#}.log"
        archiveDateFormat="yyyy-MM-dd-mm"
        archiveNumbering="Date"
        archiveEvery="Minute"
  />

Changed format to yyyy-MM-dd-mm.

This change created below files in my folder

  .
├── ...
├── LOG
│   ├── 2020-05-30.log
│   ├── OLD_LOGS
│      ├── 2020-05-30-01.log
│      └── 2020-05-30-02.log
└── ...

enter image description here

I checked with the same configuration as yours and the result was enter image description here

So the NLog archiving feature does not work the way you want it to. It just places the last file of the month to the archive folder not all.

So, you have to configure NLog to archive files every day and set max file number to something like 365 to keep them for one year. Apply below configuration:

    <target name="kFile" xsi:type="File"
        layout="${longdate} ${logger} ${message}${exception:format=ToString}"
        fileName="${basedir}/LOG/${date:format=yyyy-MM-dd}.log"
        archiveFileName="${basedir}/LOG/OLD_LOGS/{#}.log"
        archiveDateFormat="yyyy-MM-dd"
        archiveNumbering="Date"
        archiveEvery="Day"
        maxArchiveFiles="365"
  />

This way you will get one file per day and the file will be transferred to the archive folder each day. The archive will keep the file for one year. You may want to review the max archive file though. Because it may increase the size of the folder based on how much you log.

Upvotes: 2

Related Questions