4
votes

Hi I have a common Utilities class that will log some information in the class, and I will be calling this class from different classes. How should declare my Nlog so that I can separate the logger from different callers.

I have the following example where I want Nlog to keep the log produce by Utilities class within CallerA and CallerB logger accordingly, should I just pass my logger reference to Utilities class? I heard it's a good practice to keep one logger for one class.

Your advice will be much appreciated =)

namespace Common
{
    public class Utilities
    {
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public void DoSomething()
        {
            _logger.Info("log here");
        }
    }
}

namespace ProjectA
{
    public class CallerA
    {
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public void CallerMethod()
        {
            new Common.Utilities().DoSomething();
        }
    }
}

namespace ProjectB
{
    public class CallerB
    {
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public void CallerMethod()
        {
            new Common.Utilities().DoSomething();
        }
    }
}
1

1 Answers

1
votes

I would write in this case a C# extension on ILogger

So

_logger.DoSomething()