views:

368

answers:

4
  1. I want to save all logs during each day in folder named YYYYMMdd - log4net should handle creating new folder depending on system datetime - how I can setup this?
  2. I want to save all logs during the day to n files of 1MB - I don't want to rewrite old files but to really have all logs during one day - how I can setup this?

I am usin C#

Regards Alex

+1  A: 

I believe, you may create your own appender, based on the FileAppender class. You may need to override the OpenFile method to create a file in the right location.

Kerido
A: 

You could use the rollinglogfileappender which creates files named by date and starts a new file at midnight. Then just write a script that moves them to a correct map at 00.01.

As for logging all files during one day with a maximum of 1 MB per file, here's an example:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="Logging\\MWLog"/>
  <appendToFile value="true"/>
  <rollingStyle value="Composite"/>
  <datePattern value="-yyyyMMdd"/>
  <maxSizeRollBackups value="-1"/>
  <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>
Carra
+2  A: 

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>
avivo
+1  A: 

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]&#13;&#10;" />
    <footer value="[Footer]&#13;&#10;" />
    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
  </layout>
</appender>

It will create a logfile named 'logs\2010\04\02.inf.log' (let date be 2010-04-02)

he_king
Works perfectly.
Steven Wright