Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change log4net config to create every x hour new log

I still dont really get it how to change log4net if I would like to have every x hour a log. I have this:

      <log4net>
        <root>
          <level value="DEBUG"/>
          <appender-ref ref="LogFileAppender"/>
        </root>
        <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
          <param name="File" value="C:\log.txt"/>
          <param name="AppendToFile" value="true"/>
          <rollingStyle value="Size"/>
          <maxSizeRollBackups value="10"/>
          <maximumFileSize value="100MB"/>
          <staticLogFileName value="true"/>
          <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
          </layout>
        </appender>
  </log4net>

I have one log and Im adding the logs always in this one log.txt - now I would like to have lets say for every day a new log file (24hours) or maybe for every 12hours a new log. What do I have to change in my config? Any suggestions to my config? Thank you

like image 972
miri Avatar asked Aug 23 '12 22:08

miri


People also ask

What is RollingFileAppender in log4net?

RollingFileAppender means the system creates a log file based on your filters, this way you can have log files based on dates (one file each day), or get the file splitted into small chunks when it hits certain size.

What is Maxsizerollbackups in log4net?

Gets or sets the maximum number of backup files that are kept before the oldest is erased.

Does log4net support structured logging?

log4net doesn't support the concept of structured logging. Like shown in the conversionPattern element in the XML configuration, you have some variables to play with when writing to the storage. But including properties like FirstName in the Serilog example isn't available.


1 Answers

RE: http://logging.apache.org/log4net/release/config-examples.html

This example shows how to configure the RollingFileAppender to roll log files on a date period. This example will roll the log file every minute! To change the rolling period adjust the DatePattern value. For example, a date pattern of "yyyyMMdd" will roll every day. See System.Globalization.DateTimeFormatInfo for a list of available patterns.

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd-HHmm" />
<layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>

In your particular case, the above example with <datePattern value="yyyyMMdd-HH" /> would only allow you to log every hour.

However, if you wanted to log every X hour, you could create a custom appender derived from RollingFileAppender and override the AdjustFileBeforeAppend method that would check the current time against your next scheduled log interval date. See Have a Log4Net RollingFileAppender set to roll weekly for example.

like image 137
Luke Hutton Avatar answered Sep 28 '22 09:09

Luke Hutton