logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rob Richardson <RDRichard...@rad-con.com>
Subject RE: How can I change a log file name at run time?
Date Wed, 10 Jul 2013 13:19:06 GMT
Hmmm...  Do you suppose the problem could be that my program could not find the file specified
in the call to XmlConfigurator.ConfigureAndWatch()?  Naah.  I couldn't be that dumb.

Sorry for the unnecessary question.

RobR

-----Original Message-----
From: Rob Richardson [mailto:RDRichardson@rad-con.com] 
Sent: Wednesday, July 10, 2013 8:49 AM
To: Log4NET User
Subject: How can I change a log file name at run time?

This is a really common question, but I have not been able to get an answer to work.  I need
to specify the log file name at run time.  There will typically be 3 instances of the program
running at the same time.  Each will have a unique number, 1, 2 or 3, read from an external
source.  I want to be able to set the log file name to something like instance1.log, instance2.log
or instance3.log, depending on which number the program reads at run time.

I found a nice-looking example at http://www.l4ndash.com/News/tabid/69/ctl/ArticleView/mid/413/articleId/17/Programmatically-changing-log-file-names-in-Log4net.aspx
, but it is not working for me.  Here's my code:

static bool ChangeLogFileName(string AppenderName, string NewFilename)
{           
   // log4net.Repository.ILoggerRepository RootRep;
   // RootRep = log4net.LogManager.GetRepository();
   log4net.Repository.ILoggerRepository RootRep = m_logger.Logger.Repository;
   foreach (log4net.Appender.IAppender iApp in RootRep.GetAppenders())
   {
   string appenderName = iApp.Name;
   if (iApp.Name.CompareTo(AppenderName) == 0
       && iApp is log4net.Appender.FileAppender)
   {
       log4net.Appender.FileAppender fApp = (log4net.Appender.FileAppender)iApp;
       fApp.File = NewFilename;
       fApp.ActivateOptions();
       return true; // Appender found and name changed to NewFilename
   }
}
return false; // appender not found
}

I can either get the repository from my ILog object, m_logger, or I can get from the log manager.
 In either case, the result is the same:  the collection returned by the call to GetAppenders()
is empty.

Here is my configuration file:

<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="CraneUserInterface.log" />
    <appendToFile value="true" />
    <maxSizeRollBackups value="90" />
    <rollingStyle value="Size" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date - %message%newline" />
    </layout>
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFile" />
  </root>

What am I doing wrong?  How can I change my log file name at runtime?

Thanks very much!

RobR
Mime
View raw message