log4net one file per run

Log4net

Log4net Problem Overview


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.

Log4net Solutions


Solution 1 - Log4net

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

Solution 2 - Log4net

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.

Solution 3 - Log4net

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDiego MijelshonView Question on Stackoverflow
Solution 1 - Log4netStefan EgliView Answer on Stackoverflow
Solution 2 - Log4netAlexView Answer on Stackoverflow
Solution 3 - Log4netJordan StefanelliView Answer on Stackoverflow