Welcome to Hellrocker's Collections

Hope its all of some use to computer users.............

Thursday, March 17, 2011

An Ideal Log4Net configuration file

Well after receiving my first assignment on new job, I researched a lot on Google for Log4Net implementation and after 2 days of hair pulling I came up with this...

An idle configuration file to write to different log files for different types/levels of messages... check out and contact if require any clarification...

Just in your code behind use the following line, to implement the logging...

ILog ln = LogManager.GetLogger("<Logger Name from configuration>");
ln.Error(<message>, <exception>);



And use the following as the configuration settings for Log4Net... (Either put it in Web.Config or in a separate XML file)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <!-- Log4Net configuration settings -->
  <log4net debug="true">
    <appender type="log4net.Appender.RollingFileAppender" name="DebugAppender">
      <file value="DebugLog.xml" />
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="20KB"/>
      <staticLogFileName value="true"/>
      <lossy value="false" />
      <!--<layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />
      </layout>-->
      <layout type="log4net.Layout.XmlLayout">
        <param name="Prefix" value="log4net" />
      </layout>
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="DEBUG" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>

    <appender type="log4net.Appender.RollingFileAppender" name="WarnAppender">
      <file value="WarnLog.xml" />
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="20KB"/>
      <staticLogFileName value="true"/>
      <lossy value="false" />
      <!--<layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />
      </layout>-->
      <layout type="log4net.Layout.XmlLayout">
        <param name="Prefix" value="log4net" />
      </layout>
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="WARN" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>

    <appender type="log4net.Appender.RollingFileAppender" name="ErrorAppender">
      <file value="ErrorLog.xml" />
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="20KB"/>
      <staticLogFileName value="true"/>
      <lossy value="false" />
      <!--<layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />
      </layout>-->
      <layout type="log4net.Layout.XmlLayout">
        <param name="Prefix" value="log4net" />
      </layout>
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="ERROR" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>

    <appender type="log4net.Appender.RollingFileAppender" name="FatalAppender">
      <file value="FatalLog.xml" />
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="20KB"/>
      <staticLogFileName value="true"/>
      <lossy value="false" />
      <!--<layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />
      </layout>-->
      <layout type="log4net.Layout.XmlLayout">
        <param name="Prefix" value="log4net" />
      </layout>
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="FATAL" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>

    <appender type="log4net.Appender.RollingFileAppender" name="InfoAppender">
      <file value="InfoLog.xml" />
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="20KB"/>
      <staticLogFileName value="true"/>
      <lossy value="false" />
      <!--<layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />
      </layout>-->
      <layout type="log4net.Layout.XmlLayout">
        <param name="Prefix" value="log4net" />
      </layout>
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="INFO" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>

    <appender type="log4net.Appender.RollingFileAppender" name="GenericAppender">
      <file value="ALLLog.xml" />
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="20KB"/>
      <staticLogFileName value="true"/>
      <lossy value="false" />
      <!--<layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />
      </layout>-->
      <layout type="log4net.Layout.XmlLayout">
        <param name="Prefix" value="log4net" />
      </layout>
    </appender>

    <logger name="DebugLogger">
      <level value="DEBUG" />
      <appender-ref ref="DebugAppender" />
    </logger>

    <logger name="WarnLogger">
      <level value="WARN" />
      <param value="WARN" name="Threshold" />

      <appender-ref ref="WarnAppender" />
    </logger>

    <logger name="ErrorLogger">
      <level value="ERROR" />
      <param value="ERROR" name="Threshold" />

      <appender-ref ref="ErrorAppender" />
    </logger>

    <logger name="FatalLogger">
      <level value="FATAL" />
      <param value="FATAL" name="Threshold" />

      <appender-ref ref="FatalAppender" />
    </logger>

    <logger name="InfoLogger">
      <level value="INFO" />
      <param value="INFO" name="Threshold" />
     
      <appender-ref ref="InfoAppender" />
    </logger>

  </log4net>
  <!-- Log4Net configuration settings ends -->

</configuration>

0 comments:

Post a Comment