Skip to content

GetResolver is only called on first resolve #2

Open

Description

@janschreier

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.

Activity

  1. ChrisAndrews333 commented on Aug 29, 2019

    @ChrisAndrews333

    Any fix for this?

  2. KifKot commented on Feb 18, 2020

    @KifKot

    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));
    }
    
  3. janschreier commented on Feb 18, 2020

    @janschreier
    Author

    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!

  4. added a commit that references this issue on Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions