However, I seem to have two problems. First up - I dont seem to be able to initialise my connection string using the app.config, so I am forced into having it present in the log4net config section. This isnt such a problem for me now, the biggest problem is that I keep getting the following error when executing a test client:

Now, I am not using the OLEDB driver at all - I have the .Net connector installed. So why does it whinge at this? Could someone tell me if log4net works with the .Net connector for MySql? (I am specifying a provider above just for the sake of testing right now.)

I actually got past this problem on my own eventually. I wont post the code for it here just now as it isnt really in a very good state - plus there are still a few teething problems. As soon as there are sorted, I will provide my soloution.

However, I seem to have two problems. First up - I dont seem to be able to initialise my connection string using the app.config, so I am forced into having it present in the log4net config section. This isnt such a problem for me now, the biggest problem is that I keep getting the following error when executing a test client:

(The very last line is my ConsoleAppender).

Now, I am not using the OLEDB driver at all - I have the .Net connector installed. So why does it whinge at this? Could someone tell me if log4net works with the .Net connector for MySql? (I am specifying a provider above just for the sake of testing right now.)

No worries, log4net is very powerful, but quite hard to get to grips with imo, so it is only fair I share what I have been able to do...

Ok, this is my working soloution right now - with one small caveat, it generates a non fatal error.

below is some code you can use for a completee class to handle logging with log4net, it works no problems.

Code:

using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System.Linq;
using System.Text;
namespace Logger.Setup
{
public class SetUpLog
{
/*****************************************************************************/
/// <summary>
/// Use this constructor if you only want to log to the console or to a file.
/// </summary>
internal SetUpLog()
{
log4net.Config.XmlConfigurator.Configure();
}
/*****************************************************************************/
/// <summary>
/// Use this constructor if you want to log to a database. If you choose this option
/// you need to make sure that the application driving the process has the correct
/// configuration in its app.config
/// </summary>
/// <param name="dbConn">string representing the data base connection</param>
public SetUpLog(string dbConn)
{
log4net.Config.XmlConfigurator.Configure();
createAdoSettings(dbConn);
}
/*****************************************************************************/
/// <summary>
/// Defines a hierachy for use with log4net. This gets the default hierachy only.
/// </summary>
/// <returns>the default log4net hierachy</returns>
private Hierarchy hierachy()
{
Hierarchy h = (Hierarchy)log4net.LogManager.GetRepository();
return h;
}
/*****************************************************************************/
/// <summary>
/// Creates and applies some configuration to the ado appender that we will be using
/// with log4net. All we do here is basically assign the connection string.
/// </summary>
/// <returns></returns>
private AdoNetAppender adoAppender()
{
Hierarchy myHierarchy = hierachy();
if (myHierarchy != null)
{
AdoNetAppender adoApp = (AdoNetAppender)myHierarchy.Root.GetAppender("ADONetAppender");
return adoApp;
}
return null;
}
/*****************************************************************************/
/// <summary>
/// Applies the connection string and activates the options on our ADO appender.
/// Note that we can alter configuration here also - but the configuration in the app.config
/// belonging to the driving application will override any that is placed here. By altering
/// the configuration here, we loose some of the flexibility provided by log4net.
/// </summary>
/// <param name="dbConn"></param>
private void createAdoSettings(string dbConn)
{
AdoNetAppender myAppender = adoAppender();
if (myAppender != null)
{
myAppender.ConnectionString = dbConn;
myAppender.UseTransactions = true;
myAppender.ActivateOptions();
}
}
}
}

for this to work in an app, you are going to want the following code (or something very similar) in your app.config (I am assuming that if you are reading this, you have already set up your app.config for log4net):

As you can see, I have set a property with the value of "89", this is just me testing out this particular peice of functionality. As it stands right now, the log will run and update my database with the details I specify - but, there is one non-fatal error:

You see, my log runs but gives out this error and I cannot for the life of me figure out where it is coming from. I have been all over my app.config and checked it against similar examples on the web, all with no joy. I have also asked the log4net mailing list, but either noone is that interested or there is little activity on it.

I am pretty certain its just a config issue - but I cannot find out where to look to get it sorted... If anyone else has a clue of where I can look, I would be very grateful.

I am going to be blogging this soon (I would have blogged it first and posted the link, but I am not allowed to yet).

Thanks

Your post has been very helpful with getting Log4Net setup and running. I did encounter an error in the app.config file. It seems that it wants to see the parameter "connectionstring" in there, otherwise it will throw an error. I don't know if this has to do w/ the mysterious error you're seeing and trying to resolve. I enabled debugging and it complained missing that parameter or it being NULL. After I added it it, it worked w/o errors.

Originally Posted by karym6

No worries, log4net is very powerful, but quite hard to get to grips with imo, so it is only fair I share what I have been able to do...

Ok, this is my working soloution right now - with one small caveat, it generates a non fatal error.

below is some code you can use for a completee class to handle logging with log4net, it works no problems.

Code:

using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System.Linq;
using System.Text;
namespace Logger.Setup
{
public class SetUpLog
{
/*****************************************************************************/
/// <summary>
/// Use this constructor if you only want to log to the console or to a file.
/// </summary>
internal SetUpLog()
{
log4net.Config.XmlConfigurator.Configure();
}
/*****************************************************************************/
/// <summary>
/// Use this constructor if you want to log to a database. If you choose this option
/// you need to make sure that the application driving the process has the correct
/// configuration in its app.config
/// </summary>
/// <param name="dbConn">string representing the data base connection</param>
public SetUpLog(string dbConn)
{
log4net.Config.XmlConfigurator.Configure();
createAdoSettings(dbConn);
}
/*****************************************************************************/
/// <summary>
/// Defines a hierachy for use with log4net. This gets the default hierachy only.
/// </summary>
/// <returns>the default log4net hierachy</returns>
private Hierarchy hierachy()
{
Hierarchy h = (Hierarchy)log4net.LogManager.GetRepository();
return h;
}
/*****************************************************************************/
/// <summary>
/// Creates and applies some configuration to the ado appender that we will be using
/// with log4net. All we do here is basically assign the connection string.
/// </summary>
/// <returns></returns>
private AdoNetAppender adoAppender()
{
Hierarchy myHierarchy = hierachy();
if (myHierarchy != null)
{
AdoNetAppender adoApp = (AdoNetAppender)myHierarchy.Root.GetAppender("ADONetAppender");
return adoApp;
}
return null;
}
/*****************************************************************************/
/// <summary>
/// Applies the connection string and activates the options on our ADO appender.
/// Note that we can alter configuration here also - but the configuration in the app.config
/// belonging to the driving application will override any that is placed here. By altering
/// the configuration here, we loose some of the flexibility provided by log4net.
/// </summary>
/// <param name="dbConn"></param>
private void createAdoSettings(string dbConn)
{
AdoNetAppender myAppender = adoAppender();
if (myAppender != null)
{
myAppender.ConnectionString = dbConn;
myAppender.UseTransactions = true;
myAppender.ActivateOptions();
}
}
}
}

for this to work in an app, you are going to want the following code (or something very similar) in your app.config (I am assuming that if you are reading this, you have already set up your app.config for log4net):

As you can see, I have set a property with the value of "89", this is just me testing out this particular peice of functionality. As it stands right now, the log will run and update my database with the details I specify - but, there is one non-fatal error:

You see, my log runs but gives out this error and I cannot for the life of me figure out where it is coming from. I have been all over my app.config and checked it against similar examples on the web, all with no joy. I have also asked the log4net mailing list, but either noone is that interested or there is little activity on it.

I am pretty certain its just a config issue - but I cannot find out where to look to get it sorted... If anyone else has a clue of where I can look, I would be very grateful.

I am going to be blogging this soon (I would have blogged it first and posted the link, but I am not allowed to yet).