51

I need my application to create a log file each time it runs.

My preferred format would be App.log.yyyy-MM-dd_HH-mm-ss. If that's not possible, I'd settle for App.log.yyyy-MM-dd.counter

This is my current appender configuration:

<appender name="File" type="log4net.Appender.RollingFileAppender">
  <file value="App.log"/>
  <rollingStyle value="Date"/>
  <datePattern value=".yyyy-MM-dd_HH-mm-ss"/>
  <staticLogFileName value="false"/>
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>

But it creates a random number of files based on the date and time.

3 Answers 3

65

I assume that the application should create only one log file every time it runs, so you do not need a rolling file appender (though my solution would apply for rolling file appenders as well):

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file type="log4net.Util.PatternString" value="c:\temp\App-%date{yyyy-MM-dd_HH-mm-ss}.log" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%2thread] %-5level - %message%newline" />
    </layout>
</appender>

(Obviously you can use other your own layout and other settings for the file appender.)

3
  • 2
    That did the trick. I didn't know about PatternString. Thanks! Commented Jun 16, 2010 at 12:57
  • Correct me if I'm wrong, but this will create unlimited number of log files, where as the rollingstyle=Once solution will handle the purging of old files automatically. I think that solution is better.
    – habermanm
    Commented Sep 23, 2013 at 15:54
  • I guess you are right. I cannot remember if "rollingstyle=once" was possible when I gave my answer... Commented Sep 25, 2013 at 11:17
40

Also note that you can set your rolling style as

rollingstyle="Once"

and it will create a new file every time it is run. If staticLogFileName is set to true (e.g., logname.log) the previous logs will be set to logname.log.1, logname.log.2, etc.

The number of files kept before overwriting the oldest (say, 10) can be controlled by setting

maxSizeRollBackups="10"

Edit: My config, which creates a datestamped log per execution (unless one exists, in which case it follows the .1 rule, looks like this:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="Logs\MyLog-%date{dd-MM-yyyy}.log" />
    <appendToFile value="false" />    
    <maxSizeRollBackups value="-1" /> <!--infinite-->
    <staticLogFileName value="true" />
    <rollingStyle value="Once" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5level %date [%thread] %c{1} - %m%n" />
    </layout>
</appender>

Not 100% sure if I need appendToFile="false" as the docs say that's done automatically when you use rollingStyle="Once", but this makes it clearer in any case.

1
  • I have applied your way and it does create the log file with date time. But when I try to run same api again it adds the new logs into the current log file.
    – Moeez
    Commented Jul 9, 2018 at 9:30
1

It's documented from apache in the log4net docs at:

https://logging.apache.org/log4net/release/config-examples.html

ctrl+f for "per program execution"

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="logfile.txt" />
    <appendToFile value="false" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="-1" />
    <maximumFileSize value="50GB" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

Not the answer you're looking for? Browse other questions tagged or ask your own question.