Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetResolver is only called on first resolve #2

Open
janschreier opened this issue Aug 3, 2019 · 3 comments
Open

GetResolver is only called on first resolve #2

janschreier opened this issue Aug 3, 2019 · 3 comments

Comments

@janschreier
Copy link

I hope I understood the meaning of this extension correctly. I would expect to get a different logger for every class that is resolved and needs an ILogger. Currently only the first resolve leads to a new ILogger-object which is passed to all other classes that also need an ILogger.

From my understanding the following test should result in two different loggerinstances but the test fails as they are the same (just paste it into the existing test class, I was unsure if I should have made this a pull-request since I could not provide a solution):

[TestMethod]
public void NLog_different()
{
	LoggedType2 instance2 = _container.Resolve<LoggedType2>();
	Assert.AreNotSame(instance2.Logger,
		_instance.Logger);
}

public class LoggedType2
{
	public LoggedType2(ILogger log)
	{
		Logger = log;
	}
	public ILogger Logger { get; }
}

I hope I made this issue clear enough as I am fairly new to Unity.

@ChrisAndrews333
Copy link

Any fix for this?

@KifKot
Copy link

KifKot commented Feb 18, 2020

This approach has worked for me:

public ResolveDelegate<BuilderContext> GetResolver(ref BuilderContext context)
{
    return LoggerResolver;
}

private object LoggerResolver(ref BuilderContext context)
{
    var method = GetName ?? _defaultGetName;
    var declaringType = context.DeclaringType;
    return LogManager.GetLogger(method(declaringType));
}

@janschreier
Copy link
Author

janschreier commented Feb 18, 2020

I can confirm that this works like a charm and created a PR that is a bit less changing than what you suggested KifKot. Thanks for solving that issue!

janschreier added a commit to janschreier/NLog that referenced this issue Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants