Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make log4net create a new file when previous one becomes larger than file limit?

Tags:

c#

log4net

How do you make log4net create a new file when the previous one gets too large?

I have a task that takes 1 hour to complete and generates around 40MB of logs, however I want 40 files of 1MB instead of one 40MB file. I've done no configuration to log4net. All I have now is the library and that code:

private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

Then I just do:

log.Debug("some message");

How to do that?

EDIT:

<?xml version="1.0"?>
<configuration>
    <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
    <appSettings>
      <add key="mymail" value="[email protected]" />
    </appSettings>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
</configuration>

This is my app.config but I still got 40MB files :S

like image 640
gabrjan Avatar asked Dec 21 '22 11:12

gabrjan


1 Answers

<appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="F:\HornetFeed\%property{LogName}" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="-1" />
    <maximumFileSize value="5000KB" />
    <staticLogFileName value="true" />
    <countDirection value="1"/>
    <layout type="log4net.Layout.PatternLayout">
       <conversionPattern value="%m%n" />
   </layout>
   <filter type="log4net.Filter.PropertyFilter">
       <Key value="Version" />
       <StringToMatch value="1" />
   </filter>
   <filter type="log4net.Filter.DenyAllFilter" />
</appender>

This will create infinite file backups with the countdirection > 0 so that the newest file has the latest/greatest name i.e. log.5 for the newest backup (5th backup).

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

This is a Composite RollingFileAppender which keeps max of 10 1MB log backups every minute.

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net debug="false">
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
            <file value="logfile" />
            <appendToFile value="true" />
            <rollingStyle value="Composite" />
            <datePattern value=".yyyyMMdd-HHmm" />
            <maxSizeRollBackups value="10" />
            <maximumFileSize value="1MB" />
            <countDirection value="1"/>
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
            </layout>
        </appender>
        <root>
            <priority value="ALL" />
            <appender-ref ref="RollingLogFileAppender" />
        </root>
        <category name="testApp.LoggingExample"><priority value="ALL" /></category>
    </log4net>
</configuration>
like image 147
Kishore Kumar Avatar answered May 15 '23 20:05

Kishore Kumar