All I want to do is append the current date and time to my log file, say:
\"export_(Wed_Feb_21_2009_at_1_36_41PM)\"
Here is my current conf
To produce file name like:
log_2013-12-19.txt
make changes
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="log_"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="yyyy-MM-dd.\tx\t" />
<param name="StaticLogFileName" value="false"/>
</appender>
Please observe param "DatePattern" where .\tx\t makes the file name extension .txt. If you provide .txt instead of .\tx\t, then this would save file name with extension .PxP if the time is PM or .AxA in case of AM. so I used \t to enforce to write character instead of pattern. Time may also be added and what ever time pattern needed.
So, this may be really what Philipp M wanted.
Add the following to your config file
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="c:\\ProjectX\\Log\\log.txt"/>
<param name="AppendToFile" value="true"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="yyyy.MM.dd"/>
<param name="StaticLogFileName" value="true"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
If you want to use a .log suffix (to generate a file like myLogPrefix.20160309.log) use:
<appender name="GeofenceFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="C:\\Logs\\myLogPrefix" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value=".yyyyMMdd.lo'g'" />
<staticLogFileName value ="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger – %message%newline" />
</layout>
</appender>
The gotchas are the staticLogFileName = false
which allows you to generate rollups on the fly (instead of having to restart the service\server) and the awkward 'g'
in the datepattern
since g
is a magic character in datePattern
. There isn't good documentation on the log4net site to study this, so it's really wisdom gained from trial and error and scouring other people's experiences.
Use StaticLogFileName:
<param name="StaticLogFileName" value="true"/>
For those who are interested, here is the solution:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\\Users\\chris\\Documents\\log_.txt"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="_(yyyy.MM.dd-hh_mm_ss)"/>
<param name="StaticLogFileName" value="false"/>
<maximumFileSize value="100KB" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message %stackTrace%newline" />
</layout>
</appender>
and the unit test which verifies this:
[Test]
public void TestLogger()
{
logger.Info("Start Log");
for (int i = 0; i < 2500; i++)
{
logger.Info(i);
}
logger.Info("End Log Log");
}
It produces the following output:
log_.txt_(2009.02.19-01_16_34)
Not really what I wanted, but better than what I had before.