FREEWARE
LogRotator
A C# command-line tool that rotates log files with timestamp-based renaming, compression, and automatic cleanup based on configurable size limits.
LogRotator was originally created for use with One Identity Active Roles, which can produce exceptionally large log files very quickly. The tool disables logging momentarily to release the file system lock, rotates the file, then immediately re-enables logging — allowing the application to start a fresh log. There is nothing here that requires Active Roles or any other One Identity product; if you find it useful for other purposes, use it freely.
Features
- Conditional rotation — only rotates when the log file exceeds a configurable size threshold
- Timestamp-based renaming with a customisable date/time format
- ZIP and GZIP compression support
- Automatic cleanup — deletes oldest archives when total archive size exceeds a limit
- Registry toggle support — optionally disable logging/file locks before rotation
- All parameters configurable via command-line arguments or JSON config file
Download
Free to use for personal or commercial purposes. Provided as-is without warranty — see licence for full terms.
Basic usage — rotate a log file if it exceeds 10 MB:
LogRotator --log-file "C:\Logs\app.log" --rotation-threshold "10MB"
Full example — rotate if over 10 MB, keep archives under 500 MB, use ZIP compression:
# Rotate only if log exceeds 10MB, keep archives under 500MB total, use ZIP compression
LogRotator --log-file "C:\Logs\app.log" --rotation-threshold "10MB" --size-limit "500MB" --compression zip
Required arguments
Optional arguments
Size units
Rotation sequence.
Each run follows this sequence:
Size check
The tool checks whether the log file exists and whether its size exceeds the rotation threshold. If the file is below the threshold, the tool exits successfully without performing any rotation.
Registry toggle (if configured)
Reads the current registry value, sets it to the disable value to stop logging and release file locks, then waits for the configured delay period.
Rotation
The current log file is renamed with a timestamp: {original-name}_{timestamp}.{ext}
Registry restore (if configured)
Restores the original registry value to re-enable logging. This runs in a finally block — the value is always restored, even if rotation fails.
Compression
The renamed file is compressed using the specified format (ZIP or GZIP) and the uncompressed file is deleted. If compression fails, the uncompressed archived file is retained.
Cleanup
If a size limit is configured, the tool calculates the total size of all archived files. If the total exceeds the limit, it deletes oldest files until the total is below the limit.
Basic rotation
Rotate app.log if it exceeds 50 MB. Archives stored in the same directory.
LogRotator -f "app.log" -t "50MB"
With archive size limit
- Rotate app.log when it exceeds 10 MB
- Store archives in C:\Logs\Archive
- Delete oldest archives when total exceeds 500 MB
LogRotator -f "C:\Logs\app.log" -t "10MB" -s "500MB" -d "C:\Logs\Archive"
Custom date format and GZIP compression
- Rotate when file exceeds 100 MB
- Use GZIP compression
- Archived filenames use format yyyyMMdd_HHmmss
LogRotator -f "app.log" -t "100MB" -c gzip -df "yyyyMMdd_HHmmss"
With registry toggle (Active Roles / similar)
- Rotate when file exceeds 10 MB
- Set HKLM\SOFTWARE\MyApp\Logging\Enabled to 0 before rotation
- Wait 3 seconds for the logging service to release the file lock
- Restore the registry value after rotation
LogRotator -f "app.log" -t "10MB" --registry-key "HKEY_LOCAL_MACHINE\SOFTWARE\MyApp\Logging" --registry-value-name "Enabled" --registry-disable-value "0" --registry-delay 3000
Exit codes
Notes
- The archive directory is created automatically if it does not exist
- Only files matching the timestamp naming pattern are considered for cleanup
- Registry toggle requires administrator privileges for HKEY_LOCAL_MACHINE keys
- Registry values support DWORD (integers), String, and Boolean types
- File operations are synchronous to ensure data integrity
Licence
Free to use for personal or commercial purposes. Modification, reverse engineering, and redistribution are prohibited. Provided “AS IS” without warranty.