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开发者_StackOverflow社区.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
}
}
精彩评论