Using NLog as a rollover file logger

后端 未结 2 507
面向向阳花
面向向阳花 2020-12-24 03:13

How - if possible - can I use NLog as a rollover file logger? as if:

I want to have at most 31 files for 31 days and when a new day started, if there is an old day l

2条回答
  •  情书的邮戳
    2020-12-24 03:22

    I would suggest that you separate the problem into two different aspects:

    • Rolling to a new file name each day (bear in mind the time zone; UTC day perhaps?)
    • Deleting old log files

    In my experience it would be worth keeping the date in the log file name, e.g.

    debug-2010-06-08.log
    

    That part should be easy with NLog, given the examples in the docs.

    Now the second part can easily be done in a second thread or possibly even a completely different process - it just needs to see how many files are present and delete the oldest ones if necessary. Finding the "oldest" should be easy if the date is in the filename, even if you don't want to trust the file system information.

    In fact, looking at the NLog documentation, it looks like the "time-based file archival" target may do exactly what you want... but even if it doesn't, the normal "one log file per day" approach and rolling your own clean-up should be easy.

提交回复
热议问题