2

I want to configure my log4net Rolling log appender such that the name of my initial log file is:-

log_{current_datetime}.log

When the file reaches 25MB size , a new log file should be created with the same format and the previous log file should not be renamed.

How do I do such a setting on log4net appender?

2 Answers2

1

Use a PatternString for the filename:

<file type="log4net.Util.PatternString" value="log_%date{yyyyMMdd}.log" />

In order to configure rolling on size, make sure you specify RollingStyle and MaximumFileSize:

<rollingStyle value="Size"/>
<maximumFileSize value="25MB"/>

Full examples can be found here.

Update:

Unfortunately, the date specified in the filename is only evaluated upon configuration, meaning the same date will be used on each roll until log4net is reconfigured. I have not found a way to fix this.

alexleen
  • 125
  • 1
  • 2
  • 8
0

if you watch this video there are configuration samples about rollngFileAppender. my example usage is below. It creates the file every day.

link : https://www.youtube.com/watch?v=2lAdQ_QwNww

<appender name="JsonFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="c:\\Log\\.json"/>
<param name="AppendToFile" value="true"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="'SampleProjectLog_'dd.MM.yyyy"/>
<param name="StaticLogFileName" value="false"/>
<preserveLogFileNameExtension value="true" />
<layout type="OrmanSu.Altyapi.Core.CrossCuttingConcern.Logging.Log4Net.Layouts.JsonLayout" />

Okan SARICA
  • 347
  • 4
  • 15