4

I'm using log4net's AdoNetAppender in my WebAPI application. Once every few weeks the logger suddenly stops logging, and only after I restart the web service, it starts logging again. I've added to the appender a custom ErrorHandler that implements IErrorHandler, in order to catch any exceptions, like this:

public class MyErrorHandler : IErrorHandler
{
    private AdoNetAppender ParentAppender { get; set; }
    public MyErrorHandler(AdoNetAppender parentAppender)
    {
        ParentAppender = parentAppender;
    }
    public void Error(string message)
    {
        Debug.WriteLine(message);
    }

    public void Error(string message, Exception ex)
    {
        Debug.WriteLine(message + " ,Exception:" + ex.ToString());
    }

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

    }
}

, and these are the message and Exception I got:

Exception while writing to database

System.InvalidOperationException: The requested operation cannot be completed because the connection has been broken.
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName, Boolean shouldReconnect)
   at System.Data.SqlClient.SqlConnection.BeginTransaction(IsolationLevel iso, String transactionName)
   at System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)
   at System.Data.Common.DbConnection.System.Data.IDbConnection.BeginTransaction()
   at log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)

I have already set the ReconnectOnError property to true in the appender, but it doesn't make it reconnect. How can I make the appender reestablish the connection to the database, or how can I reinitialize the appender? By the way, I'm passing the appender to the ErrorHandler, so I have access to it when I catch the exception.

bjd54321
  • 422
  • 1
  • 6
  • 13
  • 1
    Can you post your appender config? There's an answer here: https://stackoverflow.com/a/38333737/5850144 that talks about adding ConnectRetryCount=0 to your connection string. Have you tried that? – tattarrattat Jun 27 '17 at 16:21
  • Thanks! That solved it. – bjd54321 Jun 28 '17 at 07:07

1 Answers1

1

I solved this by adding ConnectRetryCount=0 to my connection string as suggested by tattarrattat and https://stackoverflow.com/a/38333737/5850144 . According to MSDN, ConnectRetryCount is

The number of reconnections attempted after identifying that there was an idle connection failure.

and

Set to 0 to disable reconnecting on idle connection failures

So my guess is that by setting this to 0, the SqlClient will not try to reconnect, but rather log4net itself will try to reconnect (since I set the ReconnectOnError property to true).

bjd54321
  • 422
  • 1
  • 6
  • 13