Archive for the ‘Log4Net’ Category

 To write to a different event log  specific to your project using Log4Net EventLogAppender,you need to set the LogName property.The default value of LogName is ‘Application’ & I have changed that to CustomEventLog here.The ApplicationName property indicates the name of your application,which is the ‘Source’ column in event viewer .

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <param name="LogName" value="CustomEventLog" />
      <param name="ApplicationName" value="deeport" />
      <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%utcdate %newline %type %newline %method %newline %message %newline %exception"/>
      </layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="FATAL"/>
<levelMax value="FATAL"/>
</filter>
</appender>

 The advantage of customizing this way is,when you have to filter all event logs specific to your application it will be easy 🙂

Advertisements

For Logging purpose,I have used log4net from Apache in our project & only thing which I did not like was,if Log4Net stops logging in your system for eg due to not connecting to database,it will not throw any exception & will suppress as it is.But recently I figured out how to debug Log4Net which I will be discussing here .So all my votes goes to Log4Net & don’t want to blame them for suppressing exceptions 🙂

Appenders delegate their error handling to IErrorHandler interface.So you need to inherit a class from this interface as shown below

namespace CommonUtility.Logging
{

    //This class helps to debug Log4Net & trace Log4net issues

    class CustomErrorHandler:IErrorHandler
    {
        #region IErrorHandler Members
        public void Error(string message)
        {

        }
        public void Error(string message, Exception e)
        {

        }
        public void Error(string message, Exception e, ErrorCode errorCode)
        {

        }
        #endregion
    }
}

Insert this Tag  <errorHandler type=”CommonUtility.Logging.CustomErrorHandler”>
    </errorHandler> in your web.config,inside appender tag.

  <appender name="DBAppender" type="log4net.Appender.AdoNetAppender">
   <errorHandler type="CommonUtility.Logging.CustomErrorHandler">
   </errorHandler>
   <connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, Version=2.111.7.20, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
   <connectionString value="Data Source=CI;user ID=TEST;password=test;"/>
   <commandText value="INSERT INTO LOGGER(ID,UTCDATE,OBJECT_TYPE,METHOD,MESSAGE,EX_TEXT) VALUES(SEQ_LOG.nextval, :utcdate, :type, :method, :message,:exception)"/>
   <bufferSize value="0"/>
   <useTransactions value="false"/>
   <parameter>
    <parameterName value=":utcdate"/>
    <dbType value="DateTime"/>
    <layout type="log4net.Layout.RawUtcTimeStampLayout"/>
   </parameter>
   <parameter>
    <parameterName value=":type"/>
    <dbType value="String"/>
    <size value="200"/>
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%type"/>
    </layout>
   </parameter>
   <parameter>
    <parameterName value=":method"/>
    <dbType value="String"/>
    <size value="200"/>
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%method"/>
    </layout>
   </parameter>
   <parameter>
    <parameterName value=":message"/>
    <dbType value="String"/>
    <size value="200"/>
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%message"/>
    </layout>
   </parameter>
   <parameter>
    <parameterName value=":exception"/>
    <dbType value="String"/>
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%exception"/>
    </layout>
   </parameter>
   <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="INFO"/>
    <levelMax value="FATAL"/>
   </filter>
  </appender>

 This has worked very well for me with above Appender and should work with other appenders too.You put a breakpoint at your logging code block,say logging an info.If there is an exception like wrong oracle dll,TNS not found etc with log4net,debugger will take you to your custom class derived from interface & you can print the exception accordingly.If you try this for other appenders & find any issues,do let me know !