当前位置: 动力学知识库 > 问答 > 编程问答 >

What is the meaning of the logger name in .NET common logging?

问题描述:

I'm using the .NET common logging infrastructure V2.0. I'm getting an ILog object like so:

ILog log = LogManager.GetLogger("myLogName");

and I don't understand the meaning and effect of the log name. Does it matter if I use the same name all over the process, or can i just use LogManager.GetCurrentClassLogger() for every class (yes, I know comes with a performance penalty).

p.s:

I will probably use log4net & ConsoleOutLogger.

网友答案:

I believe name means whatever you want it to mean. There are two main reasons for using distinct names in different places:

  • You can configure logging for just that logger (e.g. turn up the log level for a particular area of code if you're investigating a problem in it)
  • You can easily see the area of code from the name, so your messages are effectively already scoped.
网友答案:

Please see GetLogger implementation below. Name is specific of concrete logger.

public sealed class LogManager
{
    ...
    public static ILoggerFactoryAdapter Adapter { get; set; }

    public static ILog GetLogger(string name)
    {
        return Adapter.GetLogger(name); // Adapter is  ILoggerFactoryAdapter
    }
}

public interface ILoggerFactoryAdapter
{
    // Methods
    ILog GetLogger(string name);
    ILog GetLogger(Type type);
}

For log4net adapter implementation looks like this

public class Log4NetLoggerFactoryAdapter : ILoggerFactoryAdapter
{
  ...
  public ILog GetLogger(string name)
  {
    return new Log4NetLogger(LogManager.GetLogger(name)); // LogManager - is log4net.Logmanager
  }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: