AdoNetAppender stops working after changes are made

Precious picture Precious · Jul 7, 2010 · Viewed 7k times · Source

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.

Answer

egrunin picture egrunin · Jul 7, 2010

First: change the buffer size to 1, not 0.

Second: if the first log message is working, your config file is fine. It's probably in the rest of your code, show us a little more.

If it's complicated, test with a very simple function or two then let us know what happens.

Edited to add

Okay, I see your code, and now I see that HexPatternConverter is being used to format one column: so something's probably going wrong there, such as throwing an exception. I suggest you try running this without using that class (just writing the Thread column as a plain string) and see if that changes anything.

Also, just in case you didn't know: if log4net encounters a SQL problem, it will fail silently. That seems unlikely in this case, because it's writing at least a couple of records, but still.

Edited to add

When you change the config file, IIS reloads the whole application, so maybe you're running out of resources. Do you have other database activity? Does it stop as well? Is it possible you're opening db connections and forgetting to close them? That would cause db activity to fail until the app restarts.

Edited to add

The ConnectionString in the config file doesn't open connections, it's just a place your program (or in this case log4net) knows to look. If you're not doing any DB activity besides log4net then it's probably not a connection problem.

Next thing to try: does log4net have this problem with a text file Appender?