views:

95

answers:

2

I'm using log4net and i configured my xml file to log into database with AdoNetAppender and evrything seems to work properly when i create the applciation and configure evrything. And i can succesfuly 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 dont 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 isnt writing to the database, coz when i try to retreive the data in my table there is no changes. I am not sure what 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 few other messages. Its the DB thats giving me headache when i change the message or i add few more messages to log.

A: 

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?

egrunin
check out i copied my class code. I posted as an answer coz i didnt know how to add the code to the comment. plz check for me!
Precious
I moved your code up into the question--you do that by clicking "edit". See my response, too.
egrunin
that class does conversion of the threadID to hexdecimal. But its not part of the question coz its working okay. I just included to show the whole code. Maybe if its even code arrangement mistakenly.
Precious
It's working okay when `log.Info` *works* ; you don't know if it's working okay when `log.Info` *fails* .
egrunin
no i dont know that. besides now whenever i make changes to the code and add few lines of code it will not respond but when i change the buffer size maybe to 1 one time and the next to 0 it will come back and show the other messages that logged in the database. I jst googled and found out the buffer holds the data before its written maybe that is where the problem lies. I just keep changing the buffer when it doesnt log to the database but i wish i could understand why do i need to keep changing the config file.
Precious
Thankz for giving me your time to answer my question. I removed the conversion class and i changed back to normal thread. But the problem persists when changes are made to the code i need to change the buffer size to get it logged in the database. I have no idea why
Precious
i dont have other database activity, but i'm not sure i closed the connections. i know i opened them in the configuration connectionString but how do i close them?
Precious
No my file appender is working perfectly no matter how many changes I make. I noticed somebody had the same problem while searching through stackoverflow questions but they failed to post the solution. I am still having the same problem but when i make changes to the buffer size i noticed the messages are shown in the database even the ones i thought they werent written to the database. any changes in the code will cause delay in writing to the database. what gives?
Precious
@egrunin Thanks Alot for your time. I apreciate it. I figured out the problem..
Precious
A: 

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.

Precious