Windows Utility
FREEWARE

LogRotator

A C# command-line tool that rotates log files with timestamp-based renaming, compression, and automatic cleanup based on configurable size limits.

Background

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.

↓ Download LogRotator
v1.0.6 · Windows x64 · ZIP

View LICENCE.txt

Quick start

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

Command reference

Required arguments

–log-file, -f <path>
Path to the log file to rotate.

–rotation-threshold, -t <size>e.g. “10MB”, “100MB”
Minimum file size before rotation occurs. Rotation only happens if the log file exceeds this size.

Optional arguments

–size-limit, -s <size>e.g. “500MB”, “1GB”
Maximum total size for archived logs. When exceeded, oldest archived files are deleted.

–archive-dir, -d <path>
Directory for archived logs. Defaults to the same directory as the log file.

–compression, -c <format>zip | gzip — default: zip
Compression format for archived files.

–date-format, -df <format>default: yyyy-MM-dd_HH-mm-ss
Date format pattern for the timestamp in archived filenames. Uses standard .NET DateTime format strings.

–registry-key, -rk <path>
Registry key path to toggle before rotation. Used to disable logging and release file locks. Requires administrator privileges for HKEY_LOCAL_MACHINE keys.

–registry-value-name, -rvn <name>
Registry value name to modify. Defaults to empty string (default value).

–registry-disable-value, -rdv <val>e.g. “0”, “false”, “Disabled”
Value to set when disabling logging. Required if –registry-key is specified.

–registry-delay, -rd <ms>default: 2000
Delay in milliseconds after setting the registry value before attempting the file move.

–help, -h
Show help message.

Size units

B, KB, MB, GB
Bytes, Kilobytes (1024 B), Megabytes (1024² B), Gigabytes (1024³ B). Examples: 10MB, 1GB, 500KB.

How it works

Rotation sequence.

Each run follows this sequence:

1

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.

2

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.

3

Rotation

The current log file is renamed with a timestamp: {original-name}_{timestamp}.{ext}

4

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.

5

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.

6

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.

Examples

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

0
Success — rotation completed, or not needed.

1
Error — file not found, invalid arguments, or other failure.

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.

View LICENCE.txt →