Log4net rolling daily filename with date in the file name
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want log4net to create a new file every day and include the date in the filename, the relevant appender is RollingFileAppender with rollingStyle="Date". The details that usually matter most are the datePattern, staticLogFileName, and whether you want the extension to stay at the end of the rolled filename.
Use RollingFileAppender with date-based rolling
A typical configuration looks like this:
With that setup, the active files will look like:
That is usually the cleanest answer to "daily rolling with the date in the filename."
Understand the important settings
The file value is the base name. It does not by itself determine the final date position. The date is inserted according to the pattern and the staticLogFileName behavior.
Key properties:
- '
rollingStyle="Date"means roll based on calendar time' - '
datePatterncontrols the inserted suffix or prefix shape' - '
staticLogFileName="true"keeps the configured base filename stable and appends the date pattern around it' - '
preserveLogFileNameExtension="true"helps keep.logat the end instead of producing awkward names'
Without preserveLogFileNameExtension, some filename patterns become less readable because the date may appear after the full filename string rather than before the extension in the style you want.
Choose the filename style intentionally
There are two common patterns people want:
app.logas the active file, with archived names derived from it- every daily file already including the date, such as
app.2025-09-23.log
For daily-only rolling, most teams prefer the second style because each file is self-describing and easy to sort. That is why datePattern="'.'yyyy-MM-dd" together with extension preservation is a common choice.
If you want a different layout, change only the pattern, for example:
which yields names like:
Make sure the appender is actually initialized
Correct XML is not enough if log4net is never configured at runtime. A minimal initialization in a .NET application might look like:
If the appender is not initialized, you can spend time tuning filename patterns while no logs are being written at all.
Common Pitfalls
The most common mistake is setting rollingStyle="Date" but leaving staticLogFileName or the datePattern in a combination that does not produce the filename style you expect. Read the final filenames, not just the XML.
Another mistake is forgetting preserveLogFileNameExtension when you want app.2025-09-23.log rather than a less intuitive arrangement.
Developers also expect log files to roll immediately when they edit the clock pattern. Rolling is time-based and depends on the appender reaching the next boundary while the process is active.
Finally, do not forget to create or grant access to the log directory. Permission issues can look like config issues if you only inspect the XML.
It is also worth verifying the actual files on disk after the first rollover. A filename pattern that looks correct in config can still produce an unexpected result if one rolling property overrides another.
Summary
- Use
RollingFileAppenderwithrollingStyle="Date"for daily log files. - Control the date in the filename with
datePattern. - Use
preserveLogFileNameExtension="true"when you want the.logsuffix to stay at the end. - Confirm runtime initialization and directory permissions as well as XML settings.
- Judge the setup by the actual filenames produced, not only by how the config looks.

