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.