Navigation and service panel


Content

Create a Single Sitecore Log File per Day

By Zahid Nawaz on 27. January 2015, No comments

This blog post is about how to prevent Sitecore from creating a new log file each time the IIS or its AppPool restarts.

If you want to know what your Sitecore instance is doing all the time then the best way is to check the log file. There you will find the information like when Sitecore instance has been started, when it builds the indexes, when caches have been created or updated, when a task has performed its job etc. Sitecore uses the Log4Net logging engine to log the information in the file system.

So far so good. In general, Sitecore creates one log file each day, but if IIS or AppPool restarts, Sitecore will create another file.

May be this is not a big issue on production server where you deploy a release once in a month or IIS/AppPool restart happened once in a day, but it seems to be a problem on your local development machine or your test server where you build and deploy your application many times a day. In this case you have to find your current log file out of hundreds of log files depends upon how the cleanup agent “Sitecore.Tasks.CleanupAgent” in your project has been equipped.

You can easily overcome this problem if you override the standard behavior of SitecoreLogFileAppender and create a new CustomLogFileAppender class which is inherited from FileAppender. Unfortunately SitecoreLogFileAppender does not offer you the possibility to override only a specific method which you need to amend for your requirements. Therefore you need to copy all code from SitecoreLogFileAppender into your newly created file appender. Now you need to remove one if-statement from following method.

Image

Here you can see the crossed if-condition which was actually responsible to create a new file each time when IIS or AppPool restarted. This particular if-condition checks if a file exists for today, than create a new one with current time stamp. Below you find the code for GetTimedFileName method.

    private string GetTimedFileName(string fileName)
    {
        int num = fileName.LastIndexOf('.');
        if (num < 0)
        {
            return fileName;
        }

        return
            string.Concat(
                new object[4]
                    {
                        (object)fileName.Substring(0, num), (object)'.', (object)this.m_currentDate.ToString("HHmmss"),
                        (object)fileName.Substring(num)
                    });
    } 

Well, now you have overridden SitecoreLogFileAppender with your CustomLogFileAppender and removed desired if-condition from OpenFile method. You have done almost everything, the only thing left behind is to configure the new CustomLogFileAppender in your configuration file. Now locate SitecoreFileAppender from your web.config and replace it with your current file appender.

It should look like as below.

Original Entry:

<appender name="LogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">

New Entry:

<appender name="LogFileAppender" type="MyProject.Core.CustomLogFileAppender, MyProject.Core">

Now everything is done, you just need to test your changes. Delete the existing log files of current day and deploy the solution on your local machine. You will see a new log file for the current day has been created. Now you can restart IIS or the AppPool as often as you want, Sitecore will not create an additional file on the same day. CustomLogFileAppender has been tested under Sitecore version 6.6.0 (rev. 130214).

You can give it a try and download the complete source of the CustomLogFileAppender class.

No comments

Add your comment

Your email address will not be published. Required fields are marked *

*