LoggingFacility Lock

The LoggingFacility documentation (http://docs.castleproject.org/Windsor.Logging-Facility.ashx) states the following:

"Lets you query an instance of ILoggerFactory to support logging for classes that are not managed by Windsor."

but how can I request an instance of ILoggerFactory if my class is not managed by Windsor ??

TIA

Søren

continued:

I'm new to using Windsor (IoC), so when you say, “Why don't you have a class manager from Windsor”, I start to wonder if you say that all classes should be managed by Windsor ...

I am working on some legacy code and should create a service interface on it ... something like:

namespace RESTServer.WindsorTestService { [ServiceContract(Namespace = "urn: RESTServer_WindsorTestService:IMyServiceClass")] public interface IMyServiceClass { ILogger Logger // Will be injected by Windsor { get; set; } [OperationContract] [WebGet(UriTemplate = "MyMethod")] int MyServiceMethod(); } [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class MyServiceClass : IMyServiceClass { private ILogger _logger = NullLogger.Instance; public ILogger Logger // Will be injected by Windsor { get { return _logger; } set { _logger = value; } } public int MyServiceMethod() { // ... which will populate val1, val2, val3 var val1 = 1; var val2 = 10; var val3 = 100; var myCalc = new myLegacyClass(val1, val2, val3); return myCalc.DoCalculate(1000); } } public class myLegacyClass { private int _val1; private int _val2; private int _val3; private static readonly ILog _logger = LogManager.GetLogger(typeof(myLegacyClass)); public myLegacyClass(int val1, int val2, int val3) { _val1 = val1; _val2 = val2; _val3 = val3; } public int DoCalculate(int val4) { _logger.Info("Doing some calculation"); return _val1 + _val2 + _val3; } } } 

where the legacy code already uses log4net .... It works fine, but I was wondering if I need to change myLegacyClass to somehow get the logger from Windsor ... I'm just not sure what the best way .....

TIA Soren

+4
source share
4 answers

container.Resolve<ILoggerFactory>() , and you yourself manage the factory and capture the registrars. The question is why: why didn't you have a Windsor class manager?

+3
source

Of course, the point of using LoggingFacility is that your registration requirements are satisfied by the Windsor configuration, and your component does not care which logger is used (if any ?!)

Jak Charloton has a good introduction here

http://devlicio.us/blogs/casey/archive/2008/06/18/logging-with-castle-windsor-the-logging-facility-and-log4net.aspx

As shown, how to use an additional injection of property dependencies to get a log instance.

If you want to get the ILogger handle from a component that is not created through the Windsor container, you can use the ServiceLocator (for example, Microsoft.Practices.ServiceLocation) configured to communicate with the Windsor container and get it that way.

i.e.

var logger = ServiceLocator.Current.GetInstance(typeof(ILogger)) as ILogger

ServiceLocator is more likely a tho 'code smell, and should be avoided if possible, and should also use the Windsor container directly.

+1
source

The question was asked: "Why don't you have a class run by Windsor." Not claiming to be a DI / IOC expert, but I understand what your domain classes are, for example. Models are usually not driven by DI / IOC. However, if one of your domain classes needs to complete some registration, then it will need ILogger.

I asked the same question, and since both solutions were labeled with a possible code smell, I wonder if there are cleaner solutions.

0
source

To answer the question, this is what I have done in the past:

 var factory = ServiceLocator.Current.GetInstance<ILoggerFactory> var logger = factory.Create(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 

This will give you what you need, but again, this is no better than referencing log4net directly in these cases.

Using IoC and working with legacy classes can sometimes hurt.

0
source

All Articles