Debugging Log4Net when it stops logging

Posted: February 18, 2011 in Log4Net
Tags: , ,

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 !

Advertisements
Comments
  1. javaGuy says:

    Good to see apache users in .NET world. I remember the original JDBC appender in log4j itself was meant to be an optional feature. (popular appenders were the file and console apenders – or in windows the event log appender).
    “DB exceptions not being logged” by default was an intentional design decision. i guess the reasoning being, logging is not a critical component and an unhandled exception in logging itself shouldn’t affect the mother application in any way. Also the idea was that it should be sort of non-intrusive and configurations altered at runtime.

    Nice blog by the way..very informative

  2. Anupama says:

    Thanks a lot for sharing this!Glad to know that you like my blog.Thank you.

  3. javaGuy says:

    By the way (just curios) any particular reason in logging into “database” ? why not windows event log or log file

    • Anupama says:

      We have large volumes of data.Not only exceptions,info’s too(millions of data entry you can expect in this case) gets logged.Hence preffered db logging for this.

  4. Kailas says:

    This article is good one. I am looking for custom error handler for Log4net.
    Finally I got this answer.
    Thank you for sharing this article.

    One Question is Log4net allow select query or any out parameter in store procedure

  5. UmpqDeveloper says:

    Not sure if you still maintain this blog…

    I’m a VB Developer and I ran into similar issue. Log4Net on one of my app stopped working. I switched from using text file to database but nothing happened. I wonder if you have the above debug code in VB? And if you have any suggestion on what I should do would be much appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s