Log4net - How to reconnect to a database after a connection failure

I am using log4net AdoNetAppender in my WebAPI application. Once every few weeks, the logger suddenly stops logging, and only after restarting the web service does it start logging again. I have added a custom ErrorHandler to the appender that implements IErrorHandler to catch any exceptions, for example:

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 this message and the Exception I received:

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 does not force it to reconnect. How can I get append to reconnect to the database or how can I reinitialize the application? By the way, I am passing the ErrorHandler application, so I have access to it when I catch the exception.

+4
1

, ConnectRetryCount = 0 , tattarrattat fooobar.com/questions/745138/.... MSDN, ConnectRetryCount

, .

0, .

, , , 0, SqlClient , log4net ( ReconnectOnError true).

+1

Source: https://habr.com/ru/post/1680222/


All Articles