0

I would like to configure log4net to have 2 different active log files (lets say debug.log and error.log) under "logs" directory and daily roll them into directories named with date (yyyyMMdd) and also change the name of the files by adding the date to their names. Here is an example directory structure output that I would like to have:

logs/
logs/debug.log
logs/error.log
logs/20180117/debug.log_20180117
logs/20180117/error.log_20180117
logs/20180116/debug.log_20180116
logs/20180116/error.log_20180116
...

Is it possible to have this with log4net? If so please share the configuration.

OziMan
  • 3
  • 4
  • you should share you config file. – jiten Jan 18 '18 at 05:49
  • possible duplicate of [Log4net rolling daily filename with date in the file name](https://stackoverflow.com/questions/1165084/) – AaA Jan 18 '18 at 07:01
  • @AaA, that is not the same as what I asked, I already tried that configuration, it adds the date also to active file, which I do not want. – OziMan Jan 18 '18 at 10:56
  • then possible duplicate of [How I can set log4net to log my files into different folders each day?](https://stackoverflow.com/questions/2385669/) – AaA Jan 19 '18 at 02:44
  • for your case `yyyyMMdd'/debug.log_'yyyyMMdd` should work – AaA Jan 19 '18 at 02:45
  • @AaA again that is not what I am asking for, it puts the active log files to dated folders with date appended. I want the name of the active file to always stay the same and under the top folder. – OziMan Jan 19 '18 at 05:55
  • I don't think it is possible with just using Log4Net. method suggested by RajN is a performance hit (checking if the file is there everytime you want to write to log while it only required once a day). We had that requirement in one project and we end up using windows scheduler to move files to folders at the end of the day. – AaA Jan 22 '18 at 02:54
  • FYI, just wanted to clarify your claim about the solution below. It is not doing file check on every write, it does only the date check. Files gets rolled once a day. I guess it is acceptable performance to do such check. – Cinchoo Jan 23 '18 at 13:37

1 Answers1

0

You going to need custom rolling appender to handle your unique requirement.

First you will have create custom appender from RollingFileAppender, override AdjustFileBeforeAppend() method as below

public class CustomRollingAppender : RollingFileAppender
{
    DateTime next = DateTime.Today;

    public CustomRollingAppender()
    {
    }

    protected override void AdjustFileBeforeAppend()
    {
        string file = File;
        DateTime newDt = DateTime.Today;
        if (next < newDt)
        {
            next = newDt.AddDays(1);

            string rollDir = Path.Combine(Path.GetDirectoryName(file), DateTime.Today.ToString("yyyyMMdd"));
            Directory.CreateDirectory(rollDir);
            string toFile = Path.Combine(rollDir, String.Format("{0}_{1}", Path.GetFileName(file), DateTime.Today.ToString("yyyyMMdd")));

            this.CloseFile();
            RollFile(file, toFile);
            SafeOpenFile(File, false);
        }
        base.AdjustFileBeforeAppend();
    }
}

Finally configure it in app.config as below

    <appender name="FileAppender" type="Log4NetTest1.CustomRollingAppender">
        <file value="logs/error.log" />
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
        </layout>
        <preserveLogFileNameExtension value="true" />
    </appender>

PS: It is not 100% tested, please test it thoroughly before put it into PROD.

Hope it helps.

Cinchoo
  • 6,088
  • 2
  • 19
  • 34