<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>
In your Log4net config file, use the following parameter with the RollingFileAppender:
<param name="DatePattern" value="dd.MM.yyyy'.log'" />
For a RollingLogFileAppender you also need these elements and values:
<rollingStyle value="Date" />
<staticLogFileName value="false" />
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" />
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
.
.
I've tried all the answers, but there was always something missing and not functioning as expected for me.
Then I experimented a bit with the hints given in each answer and was successful with the following setting:
<appender name="RollingActivityLog" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\temp\LOG4NET_Sample_Activity.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<preserveLogFileNameExtension value="true" />
<datePattern value="-yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline" />
</layout>
</appender>
The issue with other combinations of parameters was that the latest file didn't have the time pattern, or that the time pattern was appended as .log20171215
which created a new file time (and a new file type!) each day - or both issues appeared.
Now with this setting you are getting files like this one:
LOG4NET_Sample_Activity-20171215.log
which is what I wanted.
To summarize:
Don't put the date pattern in the <file value=...
attribute, just define it in the datePattern
.
Make sure you have the preserveLogFileNameExtension
value attribute set to true
.
Make sure you have the staticLogFileName
value set to false
.
Set the rollingStyle
attribute value to Date
.
To preserve file extension:
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="D:\\LogFolder\\%date{yyyyMM}\\SchT.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<maximumFileSize value="30MB" />
<staticLogFileName value="true" />
<preserveLogFileNameExtension value="true"/>
<datePattern value="ddMMyyyy" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
</log4net>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With