Created
January 19, 2011 12:57
-
-
Save dlidstrom/786131 to your computer and use it in GitHub Desktop.
How to configure Castle Windsor with logging.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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