Log4net rolling daily filename with date in the file name

150,012

Solution 1

In your Log4net config file, use the following parameter with the RollingFileAppender:

<param name="DatePattern" value="dd.MM.yyyy'.log'" />

Solution 2

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  <file value="logs\" />
  <datePattern value="dd.MM.yyyy'.log'" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="5MB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>

Solution 3

For a RollingLogFileAppender you also need these elements and values:

<rollingStyle value="Date" />
<staticLogFileName value="false" />

Solution 4

I ended up using (note the '.log' filename and the single quotes around 'myfilename_'):

  <rollingStyle value="Date" />
  <datePattern value="'myfilename_'yyyy-MM-dd"/>
  <preserveLogFileNameExtension value="true" />
  <staticLogFileName value="false" />
  <file type="log4net.Util.PatternString" value="c:\\Logs\\.log" />

This gives me:

myfilename_2015-09-22.log
myfilename_2015-09-23.log
.
.

Solution 5

Using Log4Net 1.2.13 we use the following configuration settings to allow date time in the file name.

<file type="log4net.Util.PatternString" value="E:/logname-%utcdate{yyyy-MM-dd}.txt" />

Which will provide files in the following convention: logname-2015-04-17.txt

With this it's usually best to have the following to ensure you're holding 1 log per day.

<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />

If size of file is a concern the following allows 500 files of 5MB in size until a new day spawns. CountDirection allows Ascending or Descending numbering of files which are no longer current.

 <maxSizeRollBackups value="500" />
 <maximumFileSize value="5MB" />
 <rollingStyle value="Composite" />
 <datePattern value="yyyyMMdd" />
 <CountDirection value="1"/>
 <staticLogFileName value="true" />
Share:
150,012
JL.
Author by

JL.

Developer, Designer , Coder

Updated on March 24, 2020

Comments

  • JL.
    JL. over 4 years

    I would like to have files named for example:

    dd.mm.yyyy.log

    How is this possible with log4net?

  • LostNomad311
    LostNomad311 over 12 years
    This works, but appends the date after the file extension. For example I get log files like Error.log20111104 - Does anyone know of a way to format the file name a little better?
  • LostNomad311
    LostNomad311 over 12 years
    Here's how to format the file name better: stackoverflow.com/questions/615092/…
  • nurettin
    nurettin almost 12 years
    For me, it wouldn't work without the <staticLogFileName value="false" />
  • longda
    longda about 11 years
    Appears this has been promoted to a config element: <datePattern value="dd.MM.yyyy'.log'" /> Cheers!
  • Sun
    Sun almost 10 years
    what is staticLogFileName used for? I only want the log file to be rolled into a new name after the day ends. I want to use a log monitor that looks at a specific each time.
  • NoName
    NoName over 8 years
    I think you should highlight the main point of what parameter to solve the problem as @Mun did. However, I also voted up for your answer.
  • oonyalo
    oonyalo almost 8 years
    me as well, had to set staticLogFileName to false otherwise it wouldn't log
  • Allan Ruin
    Allan Ruin almost 7 years
    why there are %date{yyyyMM} and value="ddMMyyyy" ? which is the effective date pattern?
  • Fourat
    Fourat almost 7 years
    I think datePattern is for the file name but %date{yyyyMM} is for the parent directory (I wanted it that way)
  • Michhes
    Michhes over 6 years
    It was the datePattern element, in conjunction with the staticLogFileName element (and note the value attribute for the file element) that worked for me
  • Ryan Buddicom
    Ryan Buddicom over 6 years
    The datePattern attribute sets the rolling period for Date rollingStyle. See logging.apache.org/log4net/release/config-examples.html under RollingFileAppender. "For example, a date pattern of "yyyyMMdd" will roll every day. See System.Globalization.DateTimeFormatInfo for a list of available patterns."
  • Fourat
    Fourat over 6 years
    @rbuddicom yeah but the question is about how to preserve file extension.
  • Ryan Buddicom
    Ryan Buddicom over 6 years
    I know, Allan questioned the purpose of 'value="ddMMyyyy"'. Your comment "I think datePattern is for the file name" is incorrect in that regard.
  • Fourat
    Fourat over 6 years
    @rbuddicom The datePattern indicates the pattern (or frequency) to roll the file which will be mentioned in the file name (or parent directory name if you want it). If you check the example in my answer you will see that the file is SchT.log and with <datePattern value="ddMMyyyy" /> the files will be named for ex: 201801\SchT30012018.log. Moreover, if you look closely to the comment you'll see that Allen Ruin asked why there are %date{yyyyMM} and value="ddMMyyyy" and which is the effective. I answered and explained the role of each pattern and specified that I wanted it that way.
  • Ryan Buddicom
    Ryan Buddicom over 6 years
    @Fourat fair enough :)
  • Larry Beasley
    Larry Beasley over 6 years
    @mstaessen <preserveLogFileNameExtension value="true" /> is the correct syntax and this is a great answer. How did the loss occur, may I ask?
  • mstaessen
    mstaessen over 6 years
    If I recall correctly, it went like this. On startup, log4net would generate the first filename in the rolling sequence. It would detect that that file already exist and would then decide to roll to the second file, but when that one also already exists, it does not decide to roll but instead clears it and overwrites everything in that second log file. It goes by totally unnoticed until you need logs of the cleared timeframe...
  • Simon Tewsi
    Simon Tewsi over 5 years
    You can also set rollingStyle to Composite, which rolls on both date and size.
  • Dmitry Karpenko
    Dmitry Karpenko over 5 years
    preserveLogFileNameExtension doesn't work for earlier versions of log4net (e.g. v1.2.10) as it's described here
  • Randall Flagg
    Randall Flagg about 5 years
    Works great. I would also add in the summarize: Make sure you have the staticLogFileName value set to false
  • Nagesh
    Nagesh about 4 years
    The complete answer!