/

File Logging Configuration

File logging allows you to persist logs to disk with automatic rotation and size management. Configure file paths, rotation intervals, and size limits to suit your needs.

Enable File Logging

Enable file persistence in LogSettings:

Log.Initialize(new LogSettings
{
    Persist = true,                     // Enable file logging (default: false)
    FilePath = "logs/application.log"   // File path (default: "logs.txt")
});

File Path Configuration

Basic File Path

Log.Initialize(new LogSettings
{
    Persist = true,
    FilePath = "logs/app.log"  // Simple file path
});

Directory Structure

Log.Initialize(new LogSettings
{
    Persist = true,
    FilePath = "logs/application/app.log"  // Creates directory if needed
});

Environment-Based Paths

var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";

Log.Initialize(new LogSettings
{
    Persist = true,
    FilePath = $"logs/{environment.ToLower()}/app.log"
});

File Rotation Configuration

Rolling Intervals

Configure when files are rotated:

Log.Initialize(new LogSettings
{
    Persist = true,
    FilePath = "logs/app.log",
    RollingInterval = RollingInterval.Day  // Default: Day
});

Available Intervals:

IntervalDescriptionFile Name Pattern
RollingInterval.NoneNo rotationapp.log
RollingInterval.MinuteEvery minuteapp-20241201-1430.log
RollingInterval.HourEvery hourapp-20241201-14.log
RollingInterval.DayDaily (recommended)app-20241201.log
RollingInterval.WeekWeeklyapp-2024W48.log
RollingInterval.MonthMonthlyapp-202412.log

Size-Based Rotation

Enable Size Limits

Log.Initialize(new LogSettings
{
    Persist = true,
    FilePath = "logs/app.log",
    RollOnFileSizeLimit = true,              // Enable size rotation (default: true)
    FileSizeLimitBytes = 10 * 1024 * 1024    // 10MB limit (default: 10MB)
});

Size Configuration Examples

// 5MB files
FileSizeLimitBytes = 5 * 1024 * 1024

// 50MB files  
FileSizeLimitBytes = 50 * 1024 * 1024

// 100MB files
FileSizeLimitBytes = 100 * 1024 * 1024

Combined Rotation Strategy

Log.Initialize(new LogSettings
{
    Persist = true,
    FilePath = "logs/application.log",
    
    // Time-based rotation
    RollingInterval = RollingInterval.Day,
    
    // Size-based rotation
    RollOnFileSizeLimit = true,
    FileSizeLimitBytes = 25 * 1024 * 1024  // 25MB
});

Configuration Examples

Development Environment

Log.Initialize(new LogSettings
{
    Persist = true,
    FilePath = "logs/debug.log",
    RollingInterval = RollingInterval.Hour,    // Frequent rotation for debugging
    FileSizeLimitBytes = 5 * 1024 * 1024      // Smaller files (5MB)
});

Production Environment

Log.Initialize(new LogSettings
{
    Persist = true,
    FilePath = "logs/production.log",
    RollingInterval = RollingInterval.Day,     // Daily rotation
    RollOnFileSizeLimit = true,
    FileSizeLimitBytes = 100 * 1024 * 1024    // Larger files (100MB)
});

High-Volume Applications

Log.Initialize(new LogSettings
{
    Persist = true,
    FilePath = "logs/high-volume.log",
    RollingInterval = RollingInterval.Hour,    // Hourly rotation
    RollOnFileSizeLimit = true,
    FileSizeLimitBytes = 50 * 1024 * 1024     // 50MB files
});

File Management

Automatic Directory Creation

ByteHide Logger automatically creates directories if they don't exist:

// This will create the full directory structure
FilePath = "logs/application/2024/december/app.log"

File Permissions

Ensure your application has write permissions to the log directory:

# Linux/Mac
chmod 755 logs/

# Windows - ensure write permissions for the application user

Best Practices

File Logging Best Practices

  • Use daily rotation for most applications (RollingInterval.Day)
  • Set reasonable size limits (10-50MB for most apps)
  • Use environment-specific paths to separate dev/staging/prod logs
  • Monitor disk space in production environments
  • Consider log retention policies for long-running applications

Troubleshooting

Common Issues

IssueSolution
Permission deniedCheck write permissions on log directory
Directory not foundEnable automatic directory creation or create manually
Disk space fullImplement log retention or increase rotation frequency
Files not rotatingVerify RollOnFileSizeLimit and size settings
Previous
Log Levels