Banner 468

Banner 468
Facebook
RSS

Implement logging with log4net

-
Unknown



Logging errors can be considered a very important part of software development it helps you keep a check on all the run time errors that might arise in our programs

Today i will teach you how to implement logging with the Rolling File Appender in Asp.net

The rolling file appender lets you write errors and comments into a log file plus if the log file exceeds a certain memory limit it will create another one and so on depending on the parameters you have provided.

For using Log4net you will have to download the log4net dll from the internet or you can download directly from the Package Manager Console inside visual studio.

Web.config(Inside Configuration)


  <configSections>
    <section name="log4net"
    type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net"/>
  </configSections>

 <log4net>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="LogFile.log"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyyMMdd"/>
      <appendToFile value="true"/>
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="5MB" />
      <countDirection value="1"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level %logger %date{ISO8601} - %message%newline"/>
      </layout>
      <root>
        <!-- Options are "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" and "OFF". -->
        <level value="ALL"/>
        <appender-ref ref="RollingFile"/>
      </root>
    </appender>
    </log4net>



By Default we have the following values for following properties of RollingFileAppender
  • staticLogFileName = true
  • countDirection = –1
  • rollingStyle = Composite
  • maxSizeRollBackups = 0 // be careful with this
  • maximumFileSize = “10MB”
  • datePattern = ".yyyy-MM-dd"
staticLogFileName indicates whether you need to keep writing (log) to the same file all the time. You will need to set it to false when using Date as the rolling style and you have large number of backups.
Optionally file.log.yyyy-mm-dd for current formated datePattern can by the currently logging file (or file.log.curSizeRollBackup (rollingStyle=Size) or even file.log.yyyy-mm-dd.curSizeRollBackup --- (rollingStyle=Composite)) This will make time based roll overs with a large number of backups much faster -- it won't have to rename all the backups!
Recommend to leave it at its default value “true”
countDirection when its value is –1, then newest logfile backup will always be file.log.1.. hence this would involve more number of file renaming.
By default newer files have lower numbers. (countDirection < 0) ie. log.1 is most recent, log.5 is the 5th backup, etc... countDirection > 0 does the opposite ie. log.1 is the first backup made, log.5 is the 5th backup made, etc. For infinite backups use countDirection > 0 to reduce rollOver costs.
rollingStyle can be either Date, Size or Composite. the default setting Composite, uses a combination of Size and Date settings. Thus if you have the datePattern set to “.yyyy-MM-dd” and maxSizeRollBackups set to 10, themn it will maintain 10 log backups for each day.


If you have the DatePattern set to “.yyyy-MM-dd HH:mm” and maxSizeRollbackups = 10 then it will maintain 10 logfile backups per minute

Leave a Reply