unitycontainer / log4net Goto Github PK
View Code? Open in Web Editor NEWUnity adapter for log4net
License: Apache License 2.0
Unity adapter for log4net
License: Apache License 2.0
There is an issue, when resolving a graph of objects with injecting ILog
in several places. For example see following code:
class A
{
public A(B b, ILog logger)
{
// Injected with logger.Logger.Name is "ConsoleApp1.B" too.
}
}
class B
{
public B(ILog logger)
{
// Injected with logger.Logger.Name is "ConsoleApp1.B".
}
}
Resolution code:
new UnityContainer()
.AddNewExtension<Log4NetExtension>()
.Resolve<A>();
Is it done intentionally? I'd expect to inject loggers with corresponding names in both case - A
and B
.
So I have a class that I can't pass any parameters in its constructor so instead of the usual
public class test
{
private ILog logger;
public test (ILog logger)
{
this.logger =logger;
}
}
I'm trying to achieve something like this
public class test
{
private ILog logger;
public test()
{
logger = unityContainer.Resolve<ILog>(); //
}
}
But trying it out, unity failed to resolve ILog so is there a way to solve this?
Hi, im using unity to resolve some dependencies in my windows service.
The win service starts 1 quartz job (https://github.com/quartznet/quartznet).
To resolve the job dependencies im using this extension https://github.com/hbiarge/Quartz.Unity
For some reason, the ilog dependency in the job gets resolved using the wrong logger. Instead of giving me the logger for SendByEmailJob it gives me the one used in the main service
public class SendByEmailJob : IJob
{
private readonly ILog _logger;
private readonly IDistributionLogRepository _distRepo;
public SendByEmailJob(ILog log, IDistributionLogRepository distRepo)
{
_logger = log;
_distRepo = distRepo;
}
public async Task Execute(IJobExecutionContext context)
{
_logger.Info("Starting job");
var emailDist = await _distRepo.GetAll(DeliveryType.Email, DistributionStatusType.Ready);
var dist = await _distRepo.Get(100);
foreach (var item in emailDist)
{
System.Diagnostics.Debug.WriteLine($"DistributionId = {item.Id}");
}
_logger.Info("Job completed");
}
}
So checking the Log4NetExtension class, i notice that changing the following method:
public ResolveDelegate<BuilderContext> GetResolver(ref BuilderContext context)
{
Type declaringType = context.DeclaringType;
return (ref BuilderContext c) => LogManager.GetLogger(declaringType);
}
To this:
public ResolveDelegate<BuilderContext> GetResolver(ref BuilderContext context)
{
return (ref BuilderContext c) => {
return LogManager.GetLogger(c.DeclaringType);
};
}
Solves the problem and the loggers gets correctly resolved
An example of a use case scenario and a step by step installation guide would go a long way...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.