E
E
Evgeny Glebov2015-03-31 14:44:04
C++ / C#
Evgeny Glebov, 2015-03-31 14:44:04

Logging via singleton?

Hello. There is a server to which users are connected. It follows that it is multi-threaded.
There are 2 logging methods:
1) standard (call this construct everywhere): LogManager.GetCurrentClassLogger().Debug("...");
2) mine (via singleton):
1 time:

Logger.Instance.AddLogger(new Log4NetWriter(LogManager.GetCurrentClassLogger()));

and further: Logger.Instance.Debug("...");
Can I lose something by using my method? (I'm interested in performance)
I just don't quite understand what exactly GetCurrentClassLogger does.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
veitmen, 2015-04-02
@GLeBaTi

Greetings.
Should be used when you want to use a separate logger for each class and the logger name will be the same as the class name.
Example of correct usage:

namespace MyNamespace
{
  public class MyClass
  {
    private static Logger logger = LogManager.GetCurrentClassLogger();
  }
}

Further, you always use logger in your class for logging.
More details here: https://github.com/NLog/NLog/wiki/Tutorial#creatin...
If you want to make a general logging class and use it everywhere, then make a static Logger class, implement the logging methods you need in it, which will use NLog. Those. more or less like this:
namespace MyNamespace
{
  public static class MyClass
  {
    private static Logger logger = LogManager.GetLogger("MyCoolLogger");//или упомянутый GetCurrentClassLogger()

   public static void Debug(string message){
      logger.Log(message);
   }
...
  }
}

Actually this is how you use it:
This is a simple solution, but I would not recommend using it.
I would advise using a separate logger for each class, this is BestPractise. This will make it easier for you to navigate through the logs. each logger will point to its own class.

K
Konstantin Kitmanov, 2015-03-31
@k12th

Or maybe better in general through AOP?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question