logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Karim Bourouba <kar...@hotmail.com>
Subject SystemNullReferenceException usgin log4net with MySql
Date Tue, 19 May 2009 15:24:39 GMT


Hi There,
 
I have been working with log4net now for a few weeks, after some work I have managed to use
the ADONetAppender to log to a MySQL DB. However, although I can get messages into my DB,
I am getting the following error on my console:
 

 log4net:ERROR [AdoNetAppender] Could not open database connection []
System.ArgumentNullException: Key cannot be null.
Parameter name: key
   at System.Collections.Hashtable.get_Item(Object key)
   at MySql.Data.MySqlClient.MySqlPoolManager.GetPool(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at log4net.Appender.AdoNetAppender.InitializeDatabaseConnection()
log4net:ERROR [AdoNetAppender] Could not open database connection []
System.ArgumentNullException: Key cannot be null.
Parameter name: key
   at System.Collections.Hashtable.get_Item(Object key)
   at MySql.Data.MySqlClient.MySqlPoolManager.GetPool(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at log4net.Appender.AdoNetAppender.InitializeDatabaseConnection()
 
The code for logging to my DB is as follows:
 
using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Text;
namespace BGCPLogger.LogSetup
{

public class SetUpLog
{
internal readonly ILog _log = LogManager.GetLogger(typeof(SetUpLog));
/*****************************************************************************/

internal SetUpLog()
{
log4net.Config.XmlConfigurator.Configure();
}
/*****************************************************************************/

public SetUpLog(string dbConn)
{
log4net.Config.XmlConfigurator.Configure();
createAdoSettings(dbConn);
}
/*****************************************************************************/

private Hierarchy hierachy()
{
Hierarchy h = (Hierarchy)log4net.LogManager.GetRepository();
return h;
}
/*****************************************************************************/

private AdoNetAppender adoAppender()
{
Hierarchy myHierarchy = hierachy();
if (myHierarchy != null)
{
AdoNetAppender adoApp = (AdoNetAppender)myHierarchy.Root.GetAppender("ADONetAppender");
return adoApp;
}
return null;
}
/*****************************************************************************/

private void createAdoSettings(string dbConn)
{
AdoNetAppender myAppender = adoAppender();
if (myAppender != null)
{
myAppender.ConnectionString = dbConn;
myAppender.UseTransactions = true;
myAppender.ActivateOptions();
}
}
}
}

My log4net section in my app.config looks like this:
 
<log4net> 
<root>
<level value="INFO" />
<appender-ref ref="ColoredConsoleAppender" />
<appender-ref ref="ADONetAppender" />
</root>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="1" />
<connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" />
<commandText value="insert into `tbl_lg_mlog_task_logs`(mlog_lastuser,mlog_thread,mlog_level,mlog_logger,mlog_message,mlog_exception)values('',?thread,?log_level,'My
Log',?message,?exception)" />
<parameter>
<parameterName value="?thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t" />
</layout>
</parameter>
<parameter>
<parameterName value="?log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<parameter>
<parameterName value="?logger" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c" />
</layout>
</parameter>
<parameter>
<parameterName value="?host" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%X{host}" />
</layout>
</parameter>
<parameter>
<parameterName value="?message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
</parameter>
<parameter>
<parameterName value="?exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<appender name="LogFileAppender"
type="log4net.Appender.FileAppender" >
<param name="File" value="C:\log-file.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n"/>
<param name="Footer" value="[Footer]\r\n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"
>
<mapping>
<level value="INFO" />
<foreColor value="Green, HighIntensity" />
</mapping>
<mapping>
<level value="Warn" />
<foreColor value="Cyan, HighIntensity" />
</mapping>
<mapping>
<level value="Fatal" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="Error" />
<foreColor value="Yellow, HighIntensity" />
</mapping>
<mapping>
<level value="Debug" />
<foreColor value="Cyan, HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
</layout>
</appender>
</log4net>
 
I am sure this is a fairly simple thing to fix, right now I am assuming that it is just some
missing configuration. If anyone could shed some light on this, I would be very greatful.
 
BR



_________________________________________________________________
Share your photos with Windows Live Photos – Free.
http://clk.atdmt.com/UKM/go/134665338/direct/01/
Mime
View raw message