Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net one file per run

Tags:

log4net

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.

like image 361
Diego Mijelshon Avatar asked Jun 15 '10 20:06

Diego Mijelshon


3 Answers

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.)

like image 168
Stefan Egli Avatar answered Sep 23 '22 18:09

Stefan Egli


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.

like image 40
Alex Avatar answered Sep 21 '22 18:09

Alex


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>
like image 1
Jordan Stefanelli Avatar answered Sep 21 '22 18:09

Jordan Stefanelli