Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to configure Castle Windsor with logging.
<?xml version="1.0" encoding="utf-8" ?>
<!--
This file needs to be put in the application directory. Make sure to set
'Copy to Output Directory' option in Visual Studio.
-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target xsi:type="Console" name="console" layout="${message}" />
<target xsi:type="ColoredConsole" name="console-detailed" layout="${longdate} ${logger} ${level:upperCase=true}: ${message}${newline}(${stacktrace}) ${exception:format=ToString}" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="console-detailed" />
</rules>
</nlog>
namespace CastleTest
{
using System;
using System.Linq;
using Castle.Core.Logging;
using Castle.Facilities.Logging;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using Castle.Windsor.Installer;
interface ISubProgram
{
void Run();
}
public class FirstProgram : ISubProgram
{
private ILogger logger = NullLogger.Instance;
public void Run()
{
logger.Debug("Running first program.");
}
public ILogger Logger
{
get
{
return logger;
}
set
{
logger = value;
}
}
}
public class SecondProgram : ISubProgram
{
public void Run()
{
Console.WriteLine("Running second program.");
}
}
public class SubProgramsInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
//container.Register(Component.For<SubProgram>().ImplementedBy<FirstProgram>());
container.Register(AllTypes
.FromThisAssembly()
//.IncludeNonPublicTypes()
.BasedOn<ISubProgram>()
.Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.Replace("Program", string.Empty)))
.WithService
.Base());
container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.NLog).WithConfig("nlog.config"));
}
}
public class Program
{
void Run(string[] options)
{
using (var container = new WindsorContainer())
{
try
{
// install all installers within this assembly
container.Install(FromAssembly.This());
var subProgram = container.Resolve<ISubProgram>(options[0]);
subProgram.Run();
}
catch (ComponentNotFoundException ex)
{
Usage(container, ex);
}
catch (IndexOutOfRangeException ex)
{
Usage(container, ex);
}
}
}
private static void Usage(WindsorContainer container, Exception ex)
{
var q = from c in container.ResolveAll<ISubProgram>()
select c.GetType().Name.Replace("Program", string.Empty);
Console.WriteLine("Usage: Program {0}", string.Join("|", q));
var logger = container.Resolve<ILogger>();
logger.Fatal(ex.GetType().ToString(), ex);
logger.Error(ex.GetType().ToString(), ex);
logger.Warn(ex.GetType().ToString(), ex);
logger.Info(ex.GetType().ToString(), ex);
logger.Debug(ex.GetType().ToString(), ex);
}
public static void Main(string[] options)
{
try
{
new Program().Run(options);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment