可能重复:
Configure log4net RollingFileAppender in code rolling once per application start
我正在创建两个appender如下:
private RollingFileAppender GetNewFileApender()
{
log4net.Appender.RollingFileAppender appender = new log4net.Appender.RollingFileAppender();
appender.Name = _serviceName;
appender.File = _configFileName;
appender.AppendToFile = true;
appender.MaxSizeRollBackups = 10;
appender.MaximumFileSize = "10MB";
appender.StaticLogFileName = true;
appender.RollingStyle = RollingFileAppender.RollingMode.Size;
appender.DatePattern = "yyyy.MM.dd";
appender.Layout = new log4net.Layout.PatternLayout("%d %p %m%n");
appender.ActivateOptions();
return appender;
}
它们在文件名日志文件中都有所不同。 现在我想为特定的appender获取ILog。
一般来说,我希望有两个ILog类型的变量,这样我就可以在应用程序的任何地方写入特定的日志文件。
以下是我面临的问题的完整代码:
using System;
using System.Collections;
using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;
namespace Log4netTest
{
public class Logger4net
{
private static ArrayList ServicesNames = new ArrayList();
private ILog _logger;
private string _serviceName;
private string _configFileName;
private Logger4net(string ServiceName)
{
if (ServicesNames.Contains(ServiceName))
throw new Exception(string.Format("Service {0} exists", ServiceName));
ServiceName = ServiceName.Trim();
_serviceName = ServiceName;
_configFileName = string.Format("{0}.log", _serviceName);
}
private RollingFileAppender GetNewFileApender()
{
log4net.Appender.RollingFileAppender appender = new log4net.Appender.RollingFileAppender();
appender.Name = _serviceName;
appender.File = _configFileName;
appender.AppendToFile = true;
appender.MaxSizeRollBackups = 10;
appender.MaximumFileSize = "10MB";
appender.StaticLogFileName = true;
appender.RollingStyle = RollingFileAppender.RollingMode.Size;
appender.DatePattern = "yyyy.MM.dd";
var layout = new log4net.Layout.PatternLayout("%d %p %m%n");
appender.Layout = layout;
layout.ActivateOptions();
appender.ActivateOptions();
return appender;
}
public static Logger4net AddNewLogger4net(string ServiceName)
{
Logger4net result = new Logger4net(ServiceName);
var newappender = result.GetNewFileApender();
log4net.Config.BasicConfigurator.Configure(newappender);
result._logger = log4net.LogManager.GetLogger(ServiceName);
Logger l = (Logger)result._logger.Logger;
l.AddAppender(newappender);
return result;
}
public void AddInfo(string message)
{
_logger.Info(string.Format("{0}: {1}", _serviceName, message));
}
}
class Program
{
static void Main(string[] args)
{
Logger4net log1 = Logger4net.AddNewLogger4net("Service1");
Logger4net log2 = Logger4net.AddNewLogger4net("Service2");
log1.AddInfo("Test 1");
log2.AddInfo("Test 2");
/*
in Service1.log I have:
2012-09-25 14:49:30,666 INFO Service1: Test 1
2012-09-25 14:49:30,666 INFO Service1: Test 1
2012-09-25 14:49:30,666 INFO Service2: Test 2
in Service2.log I have:
2012-09-25 14:49:30,666 INFO Service1: Test 1
2012-09-25 14:49:30,666 INFO Service2: Test 2
2012-09-25 14:49:30,666 INFO Service2: Test 2
*
* i would like to have Test 1 in Service1.log and Test 2 in Service2.log
*/
}
}
}
答案 0 :(得分:1)
使用GetLogger
类的log4net.LogManager
方法获取ILog
类型的变量,指定记录器名称或类型作为输入参数。
例如,您可以使用以下代码创建ILog静态对象:
private static readonly ILog log = LogManager.GetLogger(loggerName)
要使用特定的appender,您必须创建一个特定的记录器对象。
答案 1 :(得分:0)
我通过将Additivity设置为false解决了这个问题,这就是appender生成混合输出的原因。