logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Georg Jansen" <Georg.Jan...@FaktNet.com>
Subject RE: Database Appender
Date Fri, 03 Feb 2006 16:34:00 GMT
Raghue,

 

No that's the original code of the SendBuffer, the one  I didn't get to
work, so I wrote a modified version that I posted earlier,

I enclose my previous mail below; in case it got lost somewhere. 

 

Regards

Georg

 <http://www.l4ndash.com/> http://www.l4ndash.com Log4Net Dashboard /
Log4Net Viewer 

 

 

 

 

 

I did some modifications to the SendBuffer method of AdoNetAppender, and it
seems to work. I tested it -- starting and stopping SQLServer between log
calls.

The modified source is based on the 1.2.9 source where the config parameter
reconnectonerror is available.

 

I have kept the original "retry code" but it doesn't seems like it is
working, the m_dbConnection.State is not (at least during my test) updated
if a connection is dropped from the server.

 

 

override protected void SendBuffer(LoggingEvent[] events)

{

   if (m_reconnectOnError && (m_dbConnection == null || m_dbConnection.State
!= ConnectionState.Open))

   {

      LogLog.Debug("AdoNetAppender: Attempting to reconnect to database.
Current Connection State: " +
((m_dbConnection==null)?"<null>":m_dbConnection.State.ToString()) );

 

      InitializeDatabaseConnection();

      InitializeDatabaseCommand();

    }

 

    int NoOfReTry;

    bool Sucess = false;

 

    if (m_reconnectOnError)

    {

        NoOfReTry = 2;

    }

    else

    {

        NoOfReTry = 1;

    

    }

 

    for (int iTryNo = 1; iTryNo <= NoOfReTry && Sucess == false; iTryNo++)

    {

        try

        {

            if (iTryNo > 1) // second time? --> try to reconnect

            {

                while (iTryNo <= NoOfReTry)

                {

                    LogLog.Debug("AdoNetAppender: Attempting to reconnect to
database");

 

                    InitializeDatabaseConnection();

                    if (m_dbConnection != null && m_dbConnection.State ==
ConnectionState.Open)

                        break;

 

                    iTryNo++;

                }

 

                if (m_dbConnection == null || m_dbConnection.State !=
ConnectionState.Open)

                {

                    LogLog.Error("Giving up database reconect after trying:
" + NoOfReTry.ToString() + " times");

                    return;

                }

 

                InitializeDatabaseCommand();

            }

 

            // Check that the connection exists and is open

            if (m_dbConnection != null && m_dbConnection.State ==
ConnectionState.Open)

            {

                if (m_useTransactions)

                {

                    // Create transaction

                    // NJC - Do this on 2 lines because it can confuse the
debugger

                    IDbTransaction dbTran = null;

                    try

                    {

                        dbTran = m_dbConnection.BeginTransaction();

 

                        SendBuffer(dbTran, events);

 

                        // commit transaction

                        dbTran.Commit();

                        Sucess = true;

                    }

                    catch (Exception ex)

                    {

                        // rollback the transaction

                        if (dbTran != null)

                        {

                            try

                            {

                                dbTran.Rollback();

                            }

                            catch (Exception)

                            {

                                // Ignore exception

                            }

                        }

 

                        // Can't insert into the database. That's a bad
thing

                        ErrorHandler.Error("Exception while writing to
database", ex);

                    }

                }

                else

                {

                    // Send without transaction

                    SendBuffer(null, events);

                    Sucess = true;

                }

            }

        }

 

        catch (Exception ex)

        {

            Sucess = false;

        }

    }

}

 


Mime
View raw message