Control config file for log4net

The only reason Rob Conery likes NLog over log4net is the flexibility of config file. It’s true that by default log4net use the app config file instead of it’s own config file which NLog is following this pattern. But when I looked into the log4net’s doc, we do have way to switch this, by setting the assembly level attribute. (I still have not figure how XmlConfiguratorAttribute contorls the default XmlConfigurator.Configure() yet. ) Here is an example.

Phil found some limitations of this solution.

I think by using JP log Gateway pattern, we natually gain the flexibility to choose the config file for log4net.


public class Log4NetInitialization : ILog4NetInitialization
{
private readonly XmlElement configuration;
public Log4NetInitialization(){}

public Log4NetInitialization(string filename)
{
XmlDocument document = new XmlDocument();
document.Load(filename);
configuration = document.DocumentElement;

}
public Log4NetInitialization(XmlElement configuration)
{
this.configuration = configuration;
}

public void Execute()
{
if (configuration == null) XmlConfigurator.Configure();
else XmlConfigurator.Configure(configuration);
}
}

public class Log4NetLogFactory : ILogFactory
{
private ILog4NetInitialization initialization;
private bool initialized;

public Log4NetLogFactory(): this(new Log4NetInitialization())
{
}

public Log4NetLogFactory(string filename): this(new Log4NetInitialization(filename))
{
}

// using ILog4NetInitialization can enable logFactory read from different app config file.
public Log4NetLogFactory(ILog4NetInitialization initialization)
{
this.initialization = initialization;
}

public ILog CreateFor(Type typeThatRequiresLoggingServices)
{
WireUpConfiguration();
return new Log4NetLog(LogManager.GetLogger(typeThatRequiresLoggingServices));
}

private void WireUpConfiguration()
{
if (initialized) return;

initialization.Execute();
initialized = true;
}
}

public class Log
{
private static ILogFactory logFactory;

public static void InitializeLogFactory(ILogFactory logFactory)
{
Log.logFactory = logFactory;
}

public static ILog For(object itemThatRequiresLoggingServices)
{
return For(itemThatRequiresLoggingServices.GetType());
}

public static ILog For(Type type){

// Should use IOC here, I just tightly coupled to log4net for demo usage.
if (logFactory == null) InitializeLogFactory(new Log4NetLogFactory(SystemInfo.ApplicationBaseDirectory+"log4net.config"));

return logFactory.CreateFor(type); ;
}
}

// Log usage example
Logging.Common.Log.For(this).Debug(
string.Format("Hi there"));

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s