Search code examples
c#.netlog4netadonetappender

AdoNetAppender stops working after changes are made


I'm using log4net and I configured my XML file to log into database with AdoNetAppender and everything seems to work properly when I create the application and configure everything. And I can successfully log to the database. But when I change the message in my code then it stops logging to the database.

Here is my configuration:

<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
  <bufferSize value="0" />
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="Server=USER-PC;Integrated Security=SSPI;Initial Catalog=mydb;Trusted_Connection=true;"/>
  <commandText value="INSERT INTO Log1 ([Date],[Thread],[Level],[Logger],[Message],[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">
      <converter>
        <name value="hex_thread" />
        <type value="MyWebApplication.HexPatternConverter" />
      </converter>
      <conversionPattern value="%hex_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="4000"/>
    <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>

I found it online and did a little modification to suit my application. I'm thinking it could be because we insert into the table Log1 before getting the parameters MAYBE I really don't know because I am new to XML world and I know very little about it.

If my code is

 private static readonly ILog dblog = LogManager.GetLogger("ADONetAppender");
 dblog.Info("logging to db");

it works the first time, and then I change the message like this

dblog.Info("I AM LOGGING TO DB"); 
dblog.Info("me again");

it will not work at all, my file appenders are all right they can take the message change but my AdoNetAppender refuses this. Why is this happening?

Edited to add:

My functions are very simple I have this class to do the logging:

using System; 
using System.Collections.Generic; 
using System.Linq; using System.Web;using log4net; 
using log4net.Config; using log4net.Core;
using log4net.Layout; using System.Text; 
using System.IO; 
using log4net.Layout.Pattern; 

namespace myWebApplication 
{

public sealed class HexPatternConverter : PatternLayoutConverter
{
    override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        long id;
        if (long.TryParse(loggingEvent.ThreadName, out id))
        {
            writer.Write(id.ToString("X"));
        }
        else
        {
            writer.Write(loggingEvent.ThreadName);
        }
    }
}

public class myClass
{
    private static readonly ILog secondlog = LogManager.GetLogger("methodB");
    private static readonly ILog thirdlog = LogManager.GetLogger("methodC");
    private static readonly ILog fourthlog = LogManager.GetLogger("methodD");
    private static readonly ILog fifthlog = LogManager.GetLogger("ADONetAppender");

    public static int methodA()
    {
        int a = 0;
        return a;
    }
    public static void methodB()
    {
        methodA();
        secondlog.Info("inside method B");
    }
    public static void methodC()
    {
        methodB();
        thirdlog.Info("inside method C");
    }

    public static void methodD()
    {
        methodC();
        fourthlog.Info("inside D");
        fifthlog.Info("this is db log");
        fifthlog.Info("this is me logging to the db");
    }
}

}

The file logs are okay whenever I make changes but my DB log isn't writing to the database, because when I try to retrieve the data in my table there are no changes. I am not sure what I am doing wrong.

My Logger Configuration

<logger name="ADONetAppender">
<appender-ref ref="ADONetAppender"/>
</logger>
<logger name="methodB">
  <appender-ref ref="methodB"/>
</logger>
<logger name="methodC">
  <appender-ref ref="methodC"/>
</logger>
<logger name="methodD">
  <appender-ref ref="methodD"/>
</logger>
<root>     
</root>

The last three are RollingFileAppenders and they work perfectly wether I change the message or I add a few other messages. It's the DB that's giving me headache when I change the message or I add a few more messages to log.


Solution

  • OOH finally, its worked well... There is some code that I included in my global.asax that shouldnt be there i believe. I just tried revoming the code and it worked well. Now its instantly writing no matter how many changes i make.

    Here is the code i have no idea what it does but i just found it online. It wasnt needed in my case

     log4net.Repository.Hierarchy.Hierarchy hier = log4net.LogManager.GetRepository() as log4net.Repository.Hierarchy.Hierarchy;
    
            if (hier != null)
            {
                //get ADONetAppender
                log4net.Appender.AdoNetAppender adoAppender =
                  (log4net.Appender.AdoNetAppender)hier.GetLogger("ADONetAppender",
                    hier.LoggerFactory).GetAppender("ADONetAppender");
                if (adoAppender != null)
                {
                    adoAppender.ConnectionString =
                      System.Configuration.ConfigurationSettings.AppSettings["MyConnectionString"];
                    adoAppender.ActivateOptions(); //refresh settings of appender
                }
    
            }
    

    And after i removed it Problem solved.