jgauffin / Griffin.Framework

Application framework for Business Applications
http://griffinframework.net
168 stars 63 forks source link

Logging question #57

Closed wo80 closed 8 years ago

wo80 commented 9 years ago

I don't know if this is a feature or a bug, but please have a look at the code example below, which gives the following output:

Logger A says: ClassA is doing something.
Logger B says: ClassA is doing something.
Logger A says: ClassB is doing something.
Logger B says: ClassB is doing something.

while I would expect

Logger A says: ClassA is doing something.
Logger B says: ClassB is doing something.

This is because LogManager.GetLogger returns a CompositeLogger object containing both instances rather than the actual instance I registered. Is this intended? What would I have to change to get the desired logging behaviour?

Here's the code:

static void Main(string[] args)
{
    var loggerA = new SomeLogger(typeof(ClassA), "A");
    var loggerB = new SomeLogger(typeof(ClassB), "B");

    var provider = new LogProvider();

    provider.Add(loggerA);
    provider.Add(loggerB);

    LogManager.Provider = provider;

    var classA = new ClassA();
    var classB = new ClassB();

    classA.DoSomething();
    classB.DoSomething();

    Console.ReadLine();
}

class SomeLogger : BaseLogger
{
    string name;

    public SomeLogger(Type type, string name)
        : base(type)
    {
        this.name = name;
    }

    public override void Write(LogEntry entry)
    {
        Console.WriteLine("Logger {0} says: {1}", name, entry.Message);
    }
}

class ClassA
{
    ILogger _logger = LogManager.GetLogger<ClassA>();

    public void DoSomething()
    {
        _logger.Trace("ClassA is doing something.");
    }
}

class ClassB
{
    ILogger _logger = LogManager.GetLogger<ClassB>();

    public void DoSomething()
    {
        _logger.Trace("ClassB is doing something.");
    }
}
jgauffin commented 8 years ago

I'll take a look at it.

jgauffin commented 8 years ago

Sorry for the extremly long delay. The documentation for LogProvider states the reason:

    /// <summary>
    ///     Add a logger
    /// </summary>
    /// <param name="logger">Add a logger (will be used for all classes that want to log)</param>
    /// <remarks>
    ///     <para>
    ///         If you want to limit which classes a logger should be able to handle you need to use the other overload:
    ///         <see cref="Add(ILogger, ILoggerFilter)" />
    ///     </para>
    /// </remarks>
    public void Add(ILogger logger)