【发布时间】:2016-04-19 01:00:30
【问题描述】:
我想记录到多个文件(仅 2 个不同的文件),但将记录器名称与通常所做的类名保持相同:
private static readonly log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, isReadOnly);
我想保留类名,以便将其输出到记录器。
问题是,我需要根据被调用的 wcf 方法在附加程序之间动态切换(这是一个 wcf 服务)。我尝试了各种 log4net 配置设置,包括这个解决方案:
Configure Log4net to write to multiple files
但许多解决方案都有我不想要的预定义记录器名称。我也不想在 log4net 配置中硬编码我所有不同的类名并引用那些特定的记录器(维护噩梦)。
我正在寻找的是这样的:
public static ILog GetLogger(Type classType, bool shouldLogToFile2)
{
if (shouldLogToFile2)
{
return LogManager.GetLogger(classType, file2Appender); // log to file2.log (this statement doesn't compile)
}
else
{
return LogManager.GetLogger(classType, file1Appender); // log to file1.log (this statement doesn't compile)
}
}
然后这样称呼它:
ILog log = GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, true);
log.Info("This should log to file2.log");
【问题讨论】: