Reputation: 158
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.
2020-05-29.log
will be created in the LOG
folder.
.
├── ...
├── LOG
│ └── 2020-05-29.log
└── ...
OLD_LOGS
and a new file will be created in the LOG
folder. .
├── ...
├── LOG
│ ├── 2020-05-29.log
│ ├── OLD_LOGS
│ └── 2020-05-29.log
└── ...
2020-05-30.log
.
├── ...
├── LOG
│ ├── 2020-05-29.log
│ ├── 2020-05-30.log
│ ├── OLD_LOGS
│ └── 2020-05-29.log
└── ...
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
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
└── ...
I checked with the same configuration as yours and the result was
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