How I can set log4net to log my files into different folders each day?
Try this (It should be OK!):
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\\" />
<appendToFile value="true" />
<DatePattern value="yyyy\\\\MM\\\\dd'.inf.log'" />
<rollingStyle value="Date" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Header] " />
<footer value="[Footer] " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" />
</layout>
</appender>
It will create a logfile named 'logs\2010\04\02.inf.log' (let date be 2010-04-02)
Thank you all. We created SortByFolderFileAppender, which inherit from RollingFileAppender
Example of final result: somewhere\Logs\20100305\Client-104615.0
namespace CustomLogging
{
public class SortByFolderFileAppender : log4net.Appender.RollingFileAppender
{
protected override void OpenFile(string fileName, bool append)
{
//Inject folder [yyyyMMdd] before the file name
string baseDirectory = Path.GetDirectoryName(fileName);
string fileNameOnly = Path.GetFileName(fileName);
string newDirectory = Path.Combine(baseDirectory, DateTime.Now.ToString("yyyyMMdd"));
string newFileName = Path.Combine(newDirectory, fileNameOnly);
base.OpenFile(newFileName, append);
}
}
}
<appender name="SortByFolderFileAppender" type="CustomLogging.SortByFolderFileAppender">
<file type="log4net.Util.PatternString" value="Logs\Client"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="-HHmmss"/>
<maxSizeRollBackups value="40"/>
<maximumFileSize value="1MB"/>
<countDirection value="1"/>
<encoding value="utf-8"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss.fff}|%-5level|%message%newline"/>
</layout>
</appender>