log4net
logging
configuration
file naming
programming tips

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:

xml
1<log4net>
2  <appender name="DailyRolling" type="log4net.Appender.RollingFileAppender">
3    <file value="Logs/app.log" />
4    <appendToFile value="true" />
5    <rollingStyle value="Date" />
6    <datePattern value="'.'yyyy-MM-dd" />
7    <staticLogFileName value="true" />
8    <preserveLogFileNameExtension value="true" />
9    <layout type="log4net.Layout.PatternLayout">
10      <conversionPattern value="%date %-5level %logger - %message%newline" />
11    </layout>
12  </appender>
13
14  <root>
15    <level value="INFO" />
16    <appender-ref ref="DailyRolling" />
17  </root>
18</log4net>

With that setup, the active files will look like:

text
app.2025-09-23.log
app.2025-09-24.log

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'
  • 'datePattern controls 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 .log at 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:

  1. app.log as the active file, with archived names derived from it
  2. 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:

xml
<datePattern value="'_'yyyyMMdd" />

which yields names like:

text
app_20250923.log

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:

csharp
1using log4net;
2using log4net.Config;
3using System.IO;
4using System.Reflection;
5
6var repository = LogManager.GetRepository(Assembly.GetEntryAssembly());
7XmlConfigurator.Configure(repository, new FileInfo("App.config"));
8
9ILog log = LogManager.GetLogger(typeof(Program));
10log.Info("Application started");

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 RollingFileAppender with rollingStyle="Date" for daily log files.
  • Control the date in the filename with datePattern.
  • Use preserveLogFileNameExtension="true" when you want the .log suffix 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.

Course illustration
Course illustration

All Rights Reserved.