2

可能重复:
在每次应用程序启动时在代码滚动中配置 log4net RollingFileAppender

我正在创建两个附加程序,如下所示:

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 
      */
    }
  }
}
4

2 回答 2

1

使用类的GetLogger方法log4net.LogManager来获取ILog指定记录器名称或类型作为输入参数的类型变量。例如,您可以使用以下代码创建 ILog 静态对象:

private static readonly ILog log = LogManager.GetLogger(loggerName)

要使用特定的 appender,您必须创建特定的 logger 对象。

于 2012-09-25T11:54:51.697 回答
0

The problem I had solved by setting Additivity to false, that was the reason why appenders were generating mixed output.

于 2012-09-25T13:09:31.363 回答