Zipping your log files…

Whenever you’re making requests to a Sitecore instance, you’re generating log data. Sitecore does include a scheduled tasks using the Sitecore.Tasks.CleanupAgent to remove old log entries. However what happens if you don’t want to delete old logs? Recently I was talking to a developer who needed to keep them but also save disk space. So I hacked up a quick example of one way you might schedule zipping the logs through Sitecore. (rather than via the Windows scheduler or some other route) I’m reproducing it here in case it’s of use to anyone else.

Classes implementing code you want to schedule don’t need any special base classes or interfaces, they just need to expose a public method. To zip up old log files we need some simple code in our method to:

  • Calculate a date for the age of logs we want to archive
  • Find all the log files that fit this range
  • Create an archive and add these files
  • Remove the log files

In .Net 4.5 and later we can do this with the following simple code:

public class LogArchiver 
    public void Run()
        Sitecore.Diagnostics.Log.Info("Running log cleanup...", this);

        DateTime filterDate = DateTime.Now.AddDays(-1).Date;

        string logFolder = Path.Combine(Sitecore.Configuration.Settings.DataFolder, "logs");
        DirectoryInfo di = new DirectoryInfo(logFolder);

        var files = di.EnumerateFiles("*.txt")
            .Where(f => f.LastWriteTime <= filterDate);

            string zipFile = Path.Combine(logFolder, string.Format("LogArchive-{0}-{1}-{2}.zip", filterDate.Year, filterDate.Month, filterDate.Day));

            using (var zip = ZipFile.Open(zipFile, ZipArchiveMode.Create))
                foreach (var file in files)
                    var entry = zip.CreateEntryFromFile(file.FullName, file.Name);

The compression classes in .Net 4.5 make the code to put files into a zip simple – but it’s worth pointing out that you need to make sure you have System.IO.Compression in your using statements. The CreateEntryFromFile() method is an extension method, so the code above doesn’t compile if you use fully qualified type names when creating the ZipFile object.

Alternatively, if you’re working in older versions of the .Net framework you can use one of the third party compression libraries available such as DotNetZip or SharpZipLib.

Once you’ve put your code together it can be configured to run regularly in one of two different ways. The first is using a configuration patch:

<configuration xmlns:patch="">
      <agent type="Testing.LogArchiver" method="Run" interval="01:00:00" />

You supply the fully qualified type name along with the method name you want executed, and an execution interval in “hrs:mins:secs” format. This approach is most suitable to situations where the developers need to control the schedule of execution and it’s not expected to change much.

Alternatively you can set up the event through the Content Editor if your scenario needs the schedule to be more easily flexible. While it’s not entirely suitable for users to modify it, this approach allows changing the schedule via content rather than configuration. This involves creating Command and Schedule items in the folders under /sitecore/system/Tasks. The command again takes a fully qualified type and method name:


The schedule item references the command, and also specifies a string that defines the schedule to execute the task on:


The random looking string for the schedule data describes the date range, days of the week and interval that the task should be executed on. The details of this are explained in the scheduled task section of this blog post by John West.

And with that in place, each time your schedule fires all the log files from a day ago will be moved to a zip file:



2 thoughts on “Zipping your log files…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.