I want to configure log4net in such a manner that all the previous day logs should be archived automatically. Is there any possibility to auto archive previous logs in Log4Net. I want to do this using the configuration only without writing any code using any third party library like sharplibzip to create archive.
One more thing to add by archiving i mean compressing file in zip/rar format to save disk space.
It is not possible to archive files without writing code, sorry. However the code would not be very complicated.
You can create a custom appender inheriting from the
RollingFileAppender
, and override theAdjustFileBeforeAppend
method in order to add behavior to the file rolling. Here is the existing method that rolls the file for theRollingFileAppender
that you can override to add archiving.Use the
File
property to find the file name process itAlternatively you could find an existing appender that does what you want, but I don't know of any.
I'm taking the liberty of lifting @stuartd's comment into the answer since what he proposes is quite elegant. You can simply override the
AdjustFileBeforeAppend
in this way:It is quite a neat way of doing it, but you may want to be able to differentiate between both kind of rolls (ie date and size). For example only zipping on date roll in order to keep the files for a date range together.
This was my solution using DotNetZip reduced.
I want to point out that the only way to realize this properly is to entirely copy the
RollingFileAppender
source code and create a subclass ofFileAppender
from it. In this code, you can then add the compressing part inadjustFileBeforeAppend()
.The suggestions by samy above do not work
log4net does not contain any functionality for compressing log files. However compression is in the .Net framework (since 4.5) or you could use the Windows Shell API to zip the files, so you would have code which periodically gets all the log files which aren't the current log file and zip them up: