Castle Windsor and the LoggingFacility

   edit
Follow


So you want to be able to do some logging from your code.

 

log4net for example, is a very common logging framework for .NET, and using it is pretty straight forward, and the net is full of log4net intros.

 

Usage example:

namespace My.Application
{
	public class UsingLog4netDirectly
	{
		private static log4net.ILog logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.FullName);
		...
		...
		...
	}
}

 

However, there are some caveats with using it directly.

Windsor, once again, can help a lot with making it much easier. e.g:

namespace My.Application
{
	public class UsingLogIntegration
	{
		readonly ILogger logger;
		public UsingLogIntegration(ILogger logger)
		{
			this.logger = logger;
		}
		...
		...
		...
	}
}

 

Windsor will take care of injecting the correct logger instance, with the name set correctly (using the class name, not needing the wacky MethodBase… stuff)

 

So, what do you set this up?

  Required assemblies: Assuming you already use Windsor, you need

You actually do not need to reference these assemblies, only make sure they are on the application’s bin folder.  If you use the programmatic configuration (like I do), you’d also need the first (the facility) referenced from your code.

  Registration:

container.AddFacility("LoggingFacility", new LoggingFacility(LoggerImplementation.Log4net));

 

and …  that’s it !

 

You still need to setup log4net’s configuration, and tell the application where it is:

log4net.Config.XmlConfigurator.Configure();

  Get a different logger for tests Easy. Instead of configuring the facility, you simply configure another implementation.

container.Register(Component.For().Instance(new ConsoleLogger()));

If you’d explore the Castle.Core.Logging namespace you’d find few, very useful built-in implementations, such as a NullLogger (which you get by calling NullLogger.Instance), ConsoleLogger, StreamLogger (write to a file or memory stream), TraceLogger (writes to the diagnostic trace output) and WebLogger (writes to the HttpContext’s Trace, visible at trace.axd).


     Tweet Follow @kenegozi