在两个不同的文件中写入日志文件

时间:2009-07-08 09:04:21

标签: c# log4net

我的应用程序在自定义客户端框架上运行,客户端框架使用log4net来记录自己的日志文件。我们(我们的应用程序)必须使用相同的log4net将我们的日志文件记录在我们自己的路径中(比如我们的自定义路径)。目前我们的日志文件已创建,但日志未写入该文件。它写在客户端框架日志文件中。

我搜索了许多网站的链接 Log4Net: Programmatically specify multiple loggers (with multiple file appenders) 帮助我以编程方式配置log4net配置, 仍然我的日志语句没有写在我的日志文件中。代码如下所示

 public class TraceLog
    {
        private string message = string.Empty;        
        private static ILog ILogger = null; 
        private static TraceLog instance = new TraceLog();
        private TraceLog()
        {
            SetLevel("Log4net.MainForm", "ALL");
            AddAppender("Log4net.MainForm", CreateFileAppender("FileAppender", "C:\\mylog.log"));
        }        
        public static TraceLog Instance
        {
            get
            {
                return instance;
            }
        }      
        public void Debug(string logMessage)
        {
            message = PrepareLog(logMessage);
            ILogger.Debug(message);
        }
        protected string PrepareLog(string logMessage)
        {
            string message = GetFileMethodLineNumberInfo();
            message += logMessage;
            return message;
        }      
        protected string GetFileMethodLineNumberInfo()
        {
            StackTrace stackTrace = new StackTrace(true);
            // The position 3 is relative to the index of the specified method
            StackFrame stackFrame = stackTrace.GetFrame(3);
            return (stackFrame.GetMethod().DeclaringType.Name
                + "/" + stackFrame.GetMethod().Name
                + "/" + stackFrame.GetFileLineNumber()
                + ":");
        }
        private static void SetLevel(string loggerName, string levelName)
        {
            ILogger = LogManager.GetLogger(loggerName);
            log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
            l.Level = l.Hierarchy.LevelMap[levelName];
        }        
        private static void AddAppender(string loggerName, IAppender appender)
        {
            ILogger = LogManager.GetLogger(loggerName);
            log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
            l.AddAppender(appender);
        }
        private static IAppender CreateFileAppender(string name, string fileName)
        {
            FileAppender appender = new FileAppender();
            appender.Name = name;
            appender.File = fileName;
            appender.AppendToFile = true;
            //PatternLayout layout = new PatternLayout();

            //layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";
            //layout.ActivateOptions();
            //appender.Layout = layout;
            appender.ActivateOptions();
            return appender;
        }

    }
}

2 个答案:

答案 0 :(得分:0)

尝试调试log4net:

How to track down log4net problems

答案 1 :(得分:0)

我对这个问题的决心:

public static void AddAppenderToLogger(string loggerName, string fileName) {
    FileAppender appender = new FileAppender();
    appender.Name = string.Format("appender_{0}", loggerName);
    appender.File = fileName;
    appender.AppendToFile = true;
    appender.Layout = new PatternLayout("%date - %message%newline");
    appender.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
    appender.ActivateOptions();
    ILoggerRepository repository = LogManager.CreateRepository(string.Format("repository_{0}",loggerName));
    log4net.Config.BasicConfigurator.Configure(repository, appender);
}
public static ILog GetLogger(string loggerName){
    return LogManager.GetLogger(string.Format("repository_{0}", loggerName), loggerName);
}

//Using in application code:
    static void Main(string[] args) {
        AddAppenderToLogger("test", @"c:\testLog.txt");
        ILog log = GetLogger("test");
        log.Info("TestRecord");           
    }