sql-serverlog4netlog4net-configurationlog4net-appender

Log4Net is not logging in database after error how ever its logging in file


I have one weird issue which is coming into log4net. I have enabled the log4net with AdoDotNet appender. its works fine generally and logging into database but sometimes it suddenly stops working and not logging into database. but when we change the level info from the info to debug and again debug to info then its starts working and logging into database. I did further research and enabled the log4net internal debugging and also added RollingFileAppenderInfoErrorLogger appender in parallel to track that whether is issue is from AdonetAppender or in log4net general.

so now when this issue occurs in log4net internal debugging it is throwing below error and its stops logging into database until and unless i change something in log4net config but its keep logging into file appender
This error is transport error so my question is why log4net is not starting after some exception but still logging into file? how we can restart db logging after this kind of exception.

log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Failed in DoAppend
System.Data.SqlClient.SqlException (0x80131904): A transport-level error has occurred when sending the request to the server. (provider: Named Pipes Provider, error: 0 - The specified network name is no longer available.) ---> System.ComponentModel.Win32Exception (0x80004005): The specified network name is no longer available
   at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc, Boolean sync, TaskCompletionSource`1 completion, Int32 startRpc, Int32 startParam)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at log4net.Appender.AdoNetAppender.SendBuffer(IDbTransaction dbTran, LoggingEvent[] events)
   at log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)
   at log4net.Appender.BufferingAppenderSkeleton.Append(LoggingEvent loggingEvent)
   at log4net.Appender.AppenderSkeleton.DoAppend(LoggingEvent loggingEvent)
ClientConnectionId:a1efe1ac-6986-4a15-a233-7dcfee4ef60e
Error Number:64,State:0,Class:20

Below is my log4net config file.

<?xml version="1.0"?>
<configuration>
    <log4net debug="true">
        <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%-4timestamp [%thread] %-5level %logger - %message%newline"/>
            </layout>
        </appender>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%-4timestamp [%thread] %-5level %logger - %message%newline"/>
            </layout>
        </appender>
<appender name="RollingFileAppenderInfoErrorLogger" type="log4net.Appender.RollingFileAppender">
     <file value="D:\Halo\Logs\LoaderService.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10240KB" />
    <staticLogFileName value="true" />
    <countDirection value="1"/>
    <threshold value="INFO"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level %logger - %message%newline%newline" />
    </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
      <param name="LevelMin" value="INFO"/>
      <param name="LevelMax" value="Error"/>
    </filter>
    </appender>
        <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
            <bufferSize value="1"/>
            <usetransactions value="false"/>
            <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
            <connectionStringName value="HaloAutomationLog"/>
            <commandText value="INSERT INTO [HaloAMS].[Logging].[ApplicationLog] ([LogDate],[Thread],[LogLevel],[Logger],[LogMessage],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)"/>
            <parameter>
                <parameterName value="@log_date"/>
                <dbType value="DateTime"/>
                <layout type="log4net.Layout.RawTimeStampLayout"/>
            </parameter>
            <parameter>
                <parameterName value="@thread"/>
                <dbType value="String"/>
                <size value="255"/>
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%thread"/>
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@log_level"/>
                <dbType value="String"/>
                <size value="50"/>
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%level"/>
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@logger"/>
                <dbType value="String"/>
                <size value="255"/>
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%logger"/>
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@message"/>
                <dbType value="String"/>
                <size value="-1"/>
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%message"/>
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@exception"/>
                <dbType value="String"/>
                <size value="2000"/>
                <layout type="log4net.Layout.ExceptionLayout"/>
            </parameter>
        </appender>
        <root>
            <level value="INFO" />      
            <appender-ref ref="RollingFileAppenderInfoErrorLogger" />   
        <appender-ref ref="AdoNetAppender"/>
        </root>
    </log4net>
</configuration>

Solution

  • You can add

    <reconnectonerror value="True" />
    

    to your appender configuration or add:

    ConnectRetryCount=X; //where x is the times you want to reconnection
    

    to your connectionstring