With Serilog.Sinks.File, how do rollingInterval, fileSizeLimitBytes, rollOnFileSizeLimit and retainedFileCountLimit interact, and why can a plain WriteTo.File("log.txt") stop logging?
answer
- 1 GB default size limit
- 31 files retained, current one included
- RollingInterval.Infinite by default
- _001 suffix on size roll
- shared and buffered cannot combine
basics
~20 sSerilog.Sinks.File caps each file at 1 GB by default. Without rollingInterval or rollOnFileSizeLimit there is no roll point, so a full log.txt silently drops new events. Rolling creates a file set; retainedFileCountLimit (31) prunes the oldest.
solid answer
~40 s`WriteTo.File` defaults to `rollingInterval: RollingInterval.Infinite`, `rollOnFileSizeLimit: false`, `fileSizeLimitBytes` of 1 GB and `retainedFileCountLimit` of 31. A file that reaches the size limit stops receiving events until the next roll point; with neither rolling option set there is no next roll point, so the app stops logging to that file for good. `RollingInterval.Day` inserts the date before the extension (`callcentre-20261007.txt`), `rollOnFileSizeLimit: true` starts `_001`, `_002` files at the limit, and both can be combined. Retention applies only to a rolling file set: the oldest files beyond the count are deleted, and `retainedFileTimeLimit` adds an age limit. One process owns the file unless `shared: true`, which cannot be combined with `buffered: true`.
code
json · 16 lines{
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "logs/callcentre-.txt",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"fileSizeLimitBytes": 104857600,
"retainedFileCountLimit": 30
}
}
]
}
}go deeper
Know that RollingInterval.Day gives one file per day and that the date is inserted into the path before the extension.
Recall the defaults - 1 GB per file, 31 retained, no rolling - and that retention only applies to a rolling file set.
Diagnose the frozen 1 GB file, combine time and size rolling deliberately, and explain the shared versus buffered trade-off and the ArgumentException it triggers.
Decide whether file logging belongs on the host at all versus console output collected by the platform, and set retention against disk budgets and audit needs.
## The defaults that matter `WriteTo.File` in `Serilog.Sinks.File` 7.0 has these defaults, straight from its method signature: | Parameter | Default | Meaning | |---|---|---| | `rollingInterval` | `RollingInterval.Infinite` | never roll by time | | `rollOnFileSizeLimit` | `false` | never roll by size | | `fileSizeLimitBytes` | 1 GB | approximate cap per file; `null` removes it | | `retainedFileCountLimit` | 31 | files kept, including the current one; `null` keeps all | | `retainedFileTimeLimit` | `null` | optional age limit; ignored with `Infinite` | | `buffered` | `false` | flush after every event | | `shared` | `false` | single writer process | | `flushToDiskInterval` | `null` | no periodic full disk flush | | `outputTemplate` | `{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}` | text layout | The 1 GB cap and the 31-file retention exist, as the sink's README puts it, to avoid bringing an app down with runaway disk usage. ## Why a plain file stops growing With both rolling options off, the sink is a plain single-file sink. When `log.txt` reaches `fileSizeLimitBytes`, the sink writes the event that crosses the limit in full and then **drops further events** until the next roll point. With `Infinite` and no size rolling, there is no next roll point, so the file sits at about 1 GB while the app keeps running and logging nowhere. Nothing is thrown, because sinks must not break the application. Setting `fileSizeLimitBytes: null` removes the cap - and with it the disk protection. ## Rolling Rolling turns one path into a **file set**: 1. **By time** - `rollingInterval: RollingInterval.Day` inserts a timestamp before the extension using `yyyyMMdd` (`Hour` uses `yyyyMMddHH`, `Month` uses `yyyyMM`). A path of `logs/callcentre-.txt` yields `logs/callcentre-20261007.txt`. 2. **By size** - `rollOnFileSizeLimit: true` starts a new file when the cap is reached, adding `_001`, `_002` and so on; the first file has no number. 3. **Both** - each period's file can also split by size: `callcentre-20261007.txt`, `callcentre-20261007_001.txt`. ## Retention Retention runs only for a rolling file set. After a roll, the sink deletes the oldest files so that at most `retainedFileCountLimit` remain, the current file included. A daily set with `retainedFileCountLimit: 14` keeps two weeks - but if the set also rolls by size, 14 *files* may cover fewer than 14 days. `retainedFileTimeLimit` adds an age-based rule on top; it must not be negative and has no effect with `Infinite`. A `FileLifecycleHooks` subclass can intercept `OnFileDeleting` to archive a file before it is removed. ## Sharing, buffering and invalid combinations The configuration method validates its arguments and throws `ArgumentException` for: - `fileSizeLimitBytes` below 1; - `retainedFileCountLimit` below 1 ("At least one file must be retained"); - a negative `retainedFileTimeLimit`; - `shared: true` together with `buffered: true`; - `shared: true` together with lifecycle `hooks`. By default only one process may write the file. Two app instances pointed at the same path need `shared: true`, or better, separate paths. `buffered: true` improves throughput by letting the stream buffer writes, at the cost of events sitting in memory until a flush or disposal, which is why `Log.CloseAndFlushAsync()` matters. If the file cannot be opened at all (a permission error, for example), the sink reports it through `SelfLog` and drops events. ## Choosing a rolling policy | Need | Setting | |---|---| | one file per day, easy to find by date | `rollingInterval: RollingInterval.Day` | | bounded file size for editors and log viewers | `rollOnFileSizeLimit: true` plus a smaller `fileSizeLimitBytes` | | bounded total disk use | rolling plus `retainedFileCountLimit` (count times size is the ceiling) | | keep logs for a fixed period | rolling plus `retainedFileTimeLimit` | The text is UTF-8 without a byte-order mark unless `encoding` says otherwise. `flushToDiskInterval` forces a periodic full flush to the disk, which matters with `buffered: true` when another process tails the file. ## A call-centre production setup ```csharp .WriteTo.File("logs/callcentre-.txt", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 100 * 1024 * 1024, retainedFileCountLimit: 30) ``` In a container, most teams write to the console sink instead and let the platform collect standard output; the file sink fits VMs and on-premises servers.
- Why might retainedFileCountLimit: 14 on a daily set keep fewer than 14 days of logs?The limit counts files, not days. If `rollOnFileSizeLimit` is also on, a busy day can produce several `_001`, `_002` files, and all of them count toward the 14. Use `retainedFileTimeLimit` for an age-based guarantee, or size the limit with the split files in mind.
- Two instances of the app write to the same log path. What goes wrong and what are the options?By default one process holds the file. For a rolling set, the second instance finds the current file locked and moves on to the next sequence file (`_001`); for a plain file it fails to open, and its events are dropped with a `SelfLog` message. Give each instance its own path, or set `shared: true`, which rules out `buffered: true` and lifecycle hooks.
- The file sink grows without limit unless you configure a maximum size
- Hitting the size limit automatically starts log_001.txt even without rollOnFileSizeLimit
- retainedFileCountLimit also deletes old files for a non-rolling single file
- shared: true and buffered: true can be combined for fast multi-process logging
- Reaching the size limit throws an IOException into the application
Assumes Serilog.Sinks.File 7.0 on Serilog 4.4; the 1 GB size limit and 31-file retention defaults come from that sink's method signature.