How can I install log4net to register my files in different folders every day?

  • I want to save all the logs for each day in a folder named YYYYMMdd - log4net should handle creating a new folder depending on the system datetime - how can I configure this?
  • I want to keep all the logs during the day in n 1 MB files - I do not want to rewrite the old files, but in order to really have all the logs in one day - how can I configure this?

I am using c #

Relationship Alex

+19
c # logging configuration log4net
Mar 05 '10 at 9:29
source share
5 answers

Try it (this should be good!):

<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 log file named 'logs \ 2010 \ 04 \ 02.inf.log (let the date be 2010-04-02)

+17
Apr 2 '10 at 6:52
source share

Thanks to everyone. We created SortByFolderFileAppender which inherit from RollingFileAppender

Example end result: where \ 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> 
+10
Mar 05 '10 at 10:05
source share

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

+1
Mar 05 2018-10-10T00:
source share

You can use the rolllogfileappender file, which creates files with a name by date and launches a new file at midnight. Then just write a script that will move them to the correct map at 00.01.

Regarding the registration of all files in one day with a maximum of 1 MB per file, here is 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> 
+1
Mar 05 '10 at 9:41
source share

To build from the answer above using SortByFolderFileAppender.

Here's how we solved the problem using the calendar date for log file names. I changed staticLogFileName to true, so the whole file name is passed to the OpenFile method.

If the file name ends with ".log", then nothing needs to be added. I assume that some kind of lock occurred, and I want log4net to try to use the same file name again, hoping that the previous lock will be released.

Although, I'm not sure if this can lead to an endless call to OpenFile if the file is locked and does not release it. We created a web service using a consumer-manufacturer template to register everything in one place from all applications in the system, which currently numbers ten and is growing.

We do not need to include log4net in any of the other applications, but we need to create a web client class that is accessible to all applications that will be used to log into the web service.

The result for the file name is "Logs \ Client \ 2017 \ 11 \ 08.log" and, obviously, changes every day.

 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> 
+1
Nov 08 '17 at 2:44 on
source share



All Articles