tags:

views:

432

answers:

2

my application run on customized client framework,the client framework used log4net to log their own log files. we are(our application) has to use the same log4net to log our log files in our own path(say our customized path). currently the our log files are created but log are not writing in that file.it is writting in the client framework log file. searched lot of sites the link http://stackoverflow.com/questions/308436/log4net-programmatcially-specify-multiple-loggers-with-multiple-file-appenders helped me to configure the log4net config programatically, still im log statemets are not written in my log file.the code used as below

 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;
        }

    }
}

anyone pls help how to solve this

A: 

try debugging log4net:

http://stackoverflow.com/questions/756125/how-to-track-down-log4net-problems

despart
Hi Despart,thanks for ur info.but im not using the .config file for log4net.programmatic im doing the config file.
A: 

Hello. My resolve for this problem:

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");           
    }
Grigoryev Sergey