Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete log files after x days

I would like to log with Nlog using the file target like in this example. How can I realize a deletion of the files after X days without archiving them? Or is it possible to archive the files to the same folder?

like image 663
Freddy Avatar asked Apr 06 '11 11:04

Freddy


People also ask

How do I automatically delete files older than x days?

On Windows 10, you can use Command Prompt and Task Scheduler to automatically delete files older than a certain number of days to free up space and keep your files organized. The Settings app includes Storage sense, a feature that automatically runs when the storage is low in space.

Can I set files to delete after a certain time?

Setting a file to auto-delete button for the file and select More Actions>Set Expiration. Check off the box to Auto-delete this item on a selected date and use the box to select the appropriate date for deletion. Click Save to save your changes.


3 Answers

You could simply use the built-in archiving functionality. This setting will keep 7 old log files in addition to your current log. The cleanup is done by NLog automatically.

<?xml version="1.0" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">      <targets>         <target name="file" xsi:type="File"             layout="${longdate} ${logger} ${message}"              fileName="${basedir}/logs/logfile.txt"              archiveFileName="${basedir}/logs/log.{#}.txt"             archiveEvery="Day"             archiveNumbering="Rolling"             maxArchiveFiles="7"             concurrentWrites="true" />     </targets>      <rules>         <logger name="*" minlevel="Debug" writeTo="file" />     </rules> </nlog> 

See also the documentation of the file target

like image 200
ccellar Avatar answered Sep 21 '22 07:09

ccellar


I found that if I archive files with date-stamps in the log filenames, the archive log gets confused and {#} always translates to "0" causing old logs to never get deleted. Also, if I use a GDC reference in the log filename, it doesn't switch logs at all.

I now have to manually delete old logs if I want these fancy log filenames. The fact that they have the date in the filename causes them to automatically switch files.

// Delete log files older than X days

var dirInfo = new DirectoryInfo(".");
var oldestArchiveDate = DateTime.Now - new TimeSpan(30, 0, 0, 0);
foreach (FileInfo fi in dirInfo.GetFiles())
    if (fi.Name.StartsWith("log-") && fi.Name.EndsWith(".txt") && fi.CreationTime < oldestArchiveDate)
        fi.Delete();

var midnight = DateTime.Today.AddDays(1);
_oldLogCleanUpThread = new System.Threading.Timer(OldLogCleanUpThreadMethod, null, midnight - DateTime.Now, TimeSpan.FromDays(1));

nlog target:

 filename="${environment:variable=HOMEDRIVE}${environment:variable=HOMEPATH}\logs\log-${gdc:item=MySpecialId}-${date:format=yyyyMMdd}.txt"
GDC.Set("MySpecialId", ...);
like image 20
Curtis Avatar answered Sep 23 '22 07:09

Curtis


I don't know if this answers your question, but it looks like the maxArchiveFiles should do what you want. I have not actually used this option myself, so I can't say for sure. You can certainly "archive" your log files in the same folder.

If it were me, I would make a very small program that does some logging and set the time (archiveEvery="minute") so that it is easy to force the archiving logic to kick in. Set maxArchiveFiles to something like 5 and see if NLog keeps only 5 log files. Run your program for a while, maybe generating log messages via a timer so you can easily space the log messages over enough time that NLog's archiving/rolling logic kicks in.

Experiment with the archive file naming template. Using the archiveNumbering option gives you some control over how the archive files are numbered.

Sorry I could not give a more definitive answer or a concrete example, but I have not used those options either, so I would just have to do the same experiment(s) and I am pressed for time right now.

like image 43
wageoghe Avatar answered Sep 20 '22 07:09

wageoghe