How I can set log4net to log my files into different folders each day?
Asked Answered
S

5

22
  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

Senter answered 5/3, 2010 at 9:29 Comment(0)
A
23

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)

Acrogen answered 2/4, 2010 at 6:52 Comment(3)
what do the single quotes indicate? '.inf.log'Lemur
works perfectly, just a note, only double backslash should suffice. <file value="logs\" /> and <DatePattern value="yyyy\\MM\\dd'.inf.log'" /> should work just fine.Ianteen
I think you need to use <file value="logs\\" /> and <DatePattern value="yyyy\\\\MM\\\\dd'.inf.log'" /> to escape the `` otherwise it will escape the first 'M' and first 'd'Uropygium
S
12

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>
Senter answered 5/3, 2010 at 10:5 Comment(5)
Is it possible in OpenFile to actually append to the current log? I'd like to add a couple of messages to my custom appender that way.Arundel
I managed to get something written to stderr using LogLog.Warn/LogLog.Error but perhaps it's not possible for log4net methods to access the current log?Arundel
This solution will not work as expected if staticLogFileName is set to True. In my case, I want different folders for each day, but the same file name inside. If this property is set to true, log4net copies the file in the folder specified for the File property of the appender, and the injected folder remains empty!Maisonette
I have looked into the log4net source code, and it seems that there is no possibility to bypass what I have written above. Only put static to true, and after that post process the files and remove the date pattern from their name.Maisonette
Also, if you change some options from code on the logger, and call ActivateOptions on appender, this solution will add new folder name in the path, like: logs\23-03-2014\23-03-2014\23-03-2014... and so on with every call. Be careful with that!Maisonette
E
1

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.

Eastwards answered 5/3, 2010 at 9:34 Comment(0)
W
1

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>
Watchdog answered 5/3, 2010 at 9:41 Comment(0)
A
1

To build from the answer above using the SortByFolderFileAppender.

This is how we resolved the issue using rolling date for log filenames. I changed the staticLogFileName to true so the entire filename is passed into the OpenFile method.

If the filename ends in ".log" then nothing needs to be appended. I'm guessing some kind of locking has occurred and I want log4net to try using the same filename again, hoping the previous lock has been released.

Although, I'm not sure if this could end up causing an infinite call to OpenFile if the file is locked and won't let go of it. We did create a web service using the producer consumer pattern to log everything in one location from all applications in the system, which is currently ten and growing.

We don't need to include log4net into any of the other applications but we needed to create a web client class that is accessible by all applications to use for logging to the web service.

The result for the filename is "Logs\Client\2017\11\08.log" and obviously changes everyday.

protected override void OpenFile( string fileName, bool append )
{
    // append "\yyyy\mm\dd.log" to create the correct filename.
    if ( !fileName.EndsWith( ".log") )
        fileName = $@"{fileName}\{DateTime.Now:yyyy\\MM\\dd}.log";

    base.OpenFile( fileName, append );
}

Modification of the configuration from above.

<appender name="xxxRollingFileAppender" type="namespace.xxxRollingFileAppender">
    <file value="Logs\Client"/>
    <staticLogFileName value="true"/>
    <appendToFile value="true"/>
    <maxSizeRollBackups value="40"/>
    <maximumFileSize value="1MB"/>
    <encoding value="utf-8"/>
    <layout type="log4net.Layout.PatternLayout">
       <conversionPattern value="%date{HH:mm:ss.fff}|%-5level|%message%newline"/>
    </layout>
</appender>
Ashes answered 8/11, 2017 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.