locked
log4net file size RRS feed

  • Question

  • Just a quick question i have no idea about.

    For a Fileappender how do i specify filesize for a logfile? If i do not specifiy how big/small does the log get and how do i know when the cutoff?

    How would i delete/clear the log so it doesn't get too big?

    I know this might seem stupid but i am thinking ahead. I want only 1 file and i don't want it too big...

     

    Here is my current appender details:

              <appender name="FileAppender" type="log4net.Appender.FileAppender">
                  <file value="C:\logfile.txt" />
                  <appendToFile value="true" />
                  <layout type="log4net.Layout.PatternLayout">
                      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
                  </layout>
              </appender>

     

    • Moved by Chao Kuo Tuesday, May 4, 2010 10:28 AM Apache not C# general- (From:Visual C# General)
    Friday, April 30, 2010 2:44 PM

Answers

  • You could configure your log file to use RollingFileAppender rather than FileAppender and define a max size and when this size are reach then the older messages will be overwritten by the newest.

    In the example below you will see that the max size is 1KB and the number of backups is 0 this means that the Log4Net will keep only one log file.

     

    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="C:\logfile.txt" />
      <appendToFile value="true" />
      <maximumFileSize value="1KB" />
      <maxSizeRollBackups value="0" />
    
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
      </layout>
    </appender>

     

     

    Regards,

    Neves

    • Proposed as answer by FranklinNeves Wednesday, May 5, 2010 4:03 PM
    • Marked as answer by boris37 Wednesday, October 5, 2011 7:50 PM
    Monday, May 3, 2010 6:13 PM

All replies

  • Using FileInfo to get some Info,you have to check the size of the file directly.
    Ofcause you can use other methods,and you also store the log files.
    Do Not delete it.

    Ref.

    http://msdn.microsoft.com/en-us/library/system.io.fileinfo.length(v=VS.80).aspx

    Friday, April 30, 2010 3:41 PM
  • You could configure your log file to use RollingFileAppender rather than FileAppender and define a max size and when this size are reach then the older messages will be overwritten by the newest.

    In the example below you will see that the max size is 1KB and the number of backups is 0 this means that the Log4Net will keep only one log file.

     

    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="C:\logfile.txt" />
      <appendToFile value="true" />
      <maximumFileSize value="1KB" />
      <maxSizeRollBackups value="0" />
    
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
      </layout>
    </appender>

     

     

    Regards,

    Neves

    • Proposed as answer by FranklinNeves Wednesday, May 5, 2010 4:03 PM
    • Marked as answer by boris37 Wednesday, October 5, 2011 7:50 PM
    Monday, May 3, 2010 6:13 PM