You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-dev@logging.apache.org by "Dina Goldshtein (JIRA)" <ji...@apache.org> on 2014/06/02 08:32:01 UTC

[jira] [Created] (LOG4NET-434) Changing file path of RollingFileAppender

Dina Goldshtein created LOG4NET-434:
---------------------------------------

             Summary: Changing file path of RollingFileAppender
                 Key: LOG4NET-434
                 URL: https://issues.apache.org/jira/browse/LOG4NET-434
             Project: Log4net
          Issue Type: Bug
          Components: Appenders
    Affects Versions: 1.2.13
         Environment: Windows 7 x64, .NET 4.0, C# 5
            Reporter: Dina Goldshtein
            Priority: Minor


I have configured a rolling file appender which is supposed to roll every minute or every 10KB:

{code:xml}
        <appender name="SmsRollingFile" type="log4net.Appender.RollingFileAppender">
            <immediateFlush value="true" />
            <file type="log4net.Util.PatternString" value="${ALLUSERSPROFILE}\BSII\SMS\sms_" />
            <datePattern value="yyyyMMdd-HHmm'.log'" />
            <rollingStyle value="Composite" />
            <countDirection value="0"></countDirection>
            
            <!-- rolling based on date and file size -->
            <!--location changes on runtime-->
            <appendToFile value="true" />
            <maximumFileSize value="10KB" />
            <maxSizeRollBackups value="-1" />
            <layout type="log4net.Layout.PatternLayout">
                <param name="Header" value="======== Begin at [%date] ========%newline" type="log4net.Util.PatternString" />
                <param name="Footer" value="========= End at [%date] =========%newline%newline" type="log4net.Util.PatternString" />
                <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
            </layout>
        </appender>
{code}

Then in my main I change the path of the appender:

{code}
LogUtilities.ChangeFileAppenderPath("SmsRollingFile", "sms_pc.log");
{code}

where ChangeFileAppenderPath is defined as follows;
{code}
public static void ChangeFileAppenderPath(string appenderName, string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            ILoggerRepository repository = LogManager.GetRepository();
            foreach (IAppender appender in repository.GetAppenders())
            {
                if (appender.Name == appenderName)
                {
                    var file = appender as FileAppender;
                    if (file == null)
                    {
                        throw new ArgumentException("Must provide appender name for an appender fo type FileAppender",
                                                    "appenderName");
                    }
                    file.File = path;
                    file.ActivateOptions();
                    return;
                }
            }

            throw new ArgumentException("Appender name not valid", "appenderName");
        }
{code}

And my main method simply writes to the log in a loop;
{code}
while (true)
{
    Thread.Sleep(100);
    LogUtilities.GetInstance("Test").Debug("Test - 1 - 2 - 3");
}
{code}

Most of the time the log is written and rolled in the correct location - the working directory of the process, however, from time to time (I haven't been able to determine the conditions at which this happens) the rolled log is moved to the original location at "C:\\ProgramData\\BSII\\SMS\\.
I noticed that even after I change the file path for the appender, it still has a field called m_scheduledFilename which points to "C:\\ProgramData\\BSII\\SMS\\sms_20140602-0915.log". 



--
This message was sent by Atlassian JIRA
(v6.2#6252)