Log4Net set Max backup files on RollingFileAppender with rolling Date
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
With log4net, maxSizeRollBackups does not limit how many date-stamped log periods you keep when RollingFileAppender uses pure date-based rolling. If you need a bounded number of backups together with date information, the practical solution is usually rollingStyle="Composite", which combines date rolling with size rolling inside each date period. That distinction is the part most configurations miss.
Why Pure Date Rolling Is Different
A pure date configuration rolls when the date pattern changes, for example once per day. That gives you files such as MyApp.log.20260307, MyApp.log.20260308, and so on.
This is valid, but it does not give you a supported way to say “keep only the last ten daily files.” That is the core limitation.
What maxSizeRollBackups Actually Controls
maxSizeRollBackups is designed for size-based backup sequences. In the official log4net examples, it is used with size rolling and composite rolling. In other words, it limits numbered backup files created as the appender rolls on size.
If you combine date and size rolling, you can keep a fixed number of backups within each date period.
In that setup, log4net rolls by day and also rolls by size within the day. The maxSizeRollBackups value controls the numbered size-based backups for that date period.
What to Do If You Need Retention by Date
If your real requirement is “keep only the last N days of logs,” log4net itself is not the best place to enforce that with pure date rolling. Use one of these approaches instead:
- external log rotation tools on the host
- a scheduled cleanup job that deletes old date-stamped files
- centralized logging where local retention is intentionally short
That separates application logging from long-term file retention policy, which is often the cleaner design anyway.
It also makes retention rules easier to change later without redeploying the application just to adjust file housekeeping.
Other Important Rolling Details
Date-based rolling happens when a new log event arrives after the boundary is crossed. That means the file does not roll at midnight exactly unless something is logged after midnight.
Also be careful when changing staticLogFileName or backup-count settings on an existing log directory. Existing backup files can interact with the new configuration in surprising ways.
Common Pitfalls
- Expecting
maxSizeRollBackupsto limit the number of daily or monthly date-stamped files in pure date mode. - Using
rollingStyle="Date"when the actual requirement is “date plus bounded backup count.” - Forgetting
maximumFileSizewhen switching to composite rolling. - Assuming log4net deletes old date-period files automatically according to a retention count.
- Testing rollover only at startup rather than across a real date boundary or simulated boundary.
Summary
- Pure date rolling does not support a maximum count of retained date-period backup files.
- '
maxSizeRollBackupsapplies to size-based backup sequences.' - Use
rollingStyle="Composite"when you want date naming plus bounded size roll backups. - Use external retention cleanup if the requirement is “keep the last N days.”
- Test rollover behavior with realistic date and size conditions before relying on the configuration.

