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?
相关问题
- I want to trace logs using a Macro multi parameter
- Error message 'No handlers could be found for
- convert logback.xml to log4j.properties
- Django management command doesn't show logging
- Using Nlog to write structured logs to Google Stac
相关文章
- how do I log requests and responses for debugging
- Android Studio doesn't display logs by package
- Can I release an app without the device?
- Stacktrace does not print in Glassfish 4.1 Cluster
- Out of curiosity — why don't logging APIs impl
- Archive option greyed out in xcode 4.5.2
- Laravel log file based on date
- Java -How to get logger to work in shutdown hook?
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.
See also the documentation of the file target
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.
nlog target:
You can use the name of the day and set the
maxArchiveFiles
to a fixed number. For example, foreach day of the week you could store a max of 100 files of 100Kb: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. SetmaxArchiveFiles
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.