You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Shaily Goel <gs...@novell.com> on 2005/08/30 15:09:48 UTC

Rolling Based on Size not working

hi
 
I am facing the following problem while using RollingFileAppender:
version of Log4net used: incubating-log4net-1.2.9-beta ; Platform: Windows 

Following is the code which I am running:
 
class TestRollingFileAppender
 {
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
            log4net_Logger log = LogManager.GetLogger("TestLogger").Logger as log4net_Logger;
            log.Level = Level.Info;
            log.Additivity = false;
            log.Repository.Configured = true; 
            log.AddAppender(CreateRollingFileAppender());
            ILog logger = LogManager.GetLogger("TestLogger");
            logger.Error("This is test message: ");
            for(int i = 0; i<= 10000; i++)
            {
                logger.Error("This is new test message: " );
            }
 
  }
 
        private static RollingFileAppender CreateRollingFileAppender()
        {
           RollingFileAppender appender = new RollingFileAppender();
            MineSimpleLayout layOut = new MineSimpleLayout();
            appender.Name = "CustomRoller";
            appender.File = "C:\\TestFolder\\Roll\\roller.txt";
            appender.AppendToFile = true;           
            appender.RollingStyle = RollingFileAppender.RollingMode.Size;            
               appender.MaxSizeRollBackups = 5;
             appender.MaximumFileSize = "1KB";
                appender.Layout = layOut;
               appender.StaticLogFileName = true;
            appender.ActivateOptions();    
            return appender;
        }
        
 
 }
 
Scenario 1: No Log File is existing: Running the application for the first time:
 
I saw that when I ran my application for the first time, the rolling file appender does not roll the logs. It goes on appending the logs in current File (even when FileSize exceeds the  MaximumFileSize i.e. 1 KB in above case)
 
I digged into the code of Log4net.RollingFileAppender and found that it is never going into the method RollOverSize().
The following condition in Append method:
if ((File != null) && ((CountingQuietTextWriter)QuietWriter).Count >= m_maxFileSize) 
    {
     RollOverSize();
    }

always returns false.
 
I checked the value of ((CountingQuietTextWriter)QuietWriter).Count . It is 0 for every Log message. This means that value of ((CountingQuietTextWriter)QuietWriter).Count  is not increasing as the messages are getting appended in file and file size increasing.
 
Scenario 2: When the Log File is already existing
 
I found that at the start it checks the size of already existing file and properly set the value of ((CountingQuietTextWriter)QuietWriter).Count equal to FileSize.
Now when the message is logged and if  the value of  ((CountingQuietTextWriter)QuietWriter).Count is greater then m_maxFileSize  the File is rolled properly and (CountingQuietTextWriter)QuietWriter).Count  is again set to "0". Now The new messages are being written into the current file but as ((CountingQuietTextWriter)QuietWriter).Count  is never increasing so "No" Rolls takes place after that.
 
Conclusion : ((CountingQuietTextWriter)QuietWriter).Count  is only set at initial time of application. After that it never increments as the size of file grows.
 
My requirement is as the FileSize grows above the particular limit the Roll of file should be taken.
 
Please let me know if this is bug in Log4net or I am missing some thing.
 
Thanks
Shaily