I'm trying to configure Unity to inject an ILog into my classes, where the type/name in LogManager.CreateLogger() is set as the class where ILog is being injected into.
Here is a similar question, but for Structure Map
I know this answer is very late, but I've recently had this issue.
There is now a NuGet package, UnityLog4NetExtension, that registers the log4net.ILog
interface and some Unity build strategies that allows you to just inject an ILog
interface and have Unity create it using the class that it is being injected in to.
The code is also available via GitHub.
For example:
Add the extension to the unity container, before you need to inject ILog instancies. I've done this in the bootstrapper.
//add the extension to the unity container
container.AddNewExtension<Log4NetExtension>();
Then, just inject the ILog
interface and it will work
public class MyClass
{
private readonly ILog _logger;
public MyClass(ILog loggerForMyClass)
{
_logger = loggerForMyClass;
}
}
This code works just as if I'd written _logger = LogManager.GetLogger(typeof(MyClass))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With