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.