🔷 🎯 Best Practices - TuskLang for C# - "Production Excellence"

C# Documentation

🎯 Best Practices - TuskLang for C# - "Production Excellence"

Master the art of TuskLang in production - From code quality to operational excellence!

Best practices are the foundation of successful TuskLang implementations. Learn the patterns, strategies, and techniques that separate good configurations from great ones in production environments.

🎯 Best Practices Philosophy

"We Don't Bow to Any King"

- Code quality - Clean, maintainable, and readable configurations - Security first - Protect sensitive data and prevent vulnerabilities - Performance optimization - Efficient and scalable configurations - Operational excellence - Monitoring, logging, and troubleshooting - Team collaboration - Consistent patterns and shared knowledge

Why Best Practices Matter?

- Maintainability - Easy to understand and modify configurations - Security - Protect against data breaches and vulnerabilities - Performance - Optimize for speed and resource efficiency - Reliability - Reduce errors and improve system stability - Scalability - Handle growth and increased load

📝 Configuration Best Practices

1. Structure and Organization

Good: Well-organized configuration structure

Global variables at the top

$app_name: "TuskLangApp" $version: "1.0.0" $environment: @env("APP_ENV", "development")

Application configuration

[app] name: $app_name version: $version environment: $environment debug: @if($environment != "production", true, false)

Database configuration

[database] primary { host: @env("DB_PRIMARY_HOST", "localhost") port: @env("DB_PRIMARY_PORT", "5432") name: @env("DB_PRIMARY_NAME", "tuskapp") user: @env("DB_PRIMARY_USER", "postgres") password: @env.secure("DB_PRIMARY_PASSWORD") ssl: @if($environment == "production", true, false) }

Feature flags

[features] cache_enabled: true ml_enabled: @if($environment == "production", true, false) debug_mode: @if($environment != "production", true, false)

Bad: Poorly organized configuration

[app] name: "TuskLangApp" version: "1.0.0" debug: true

[database] host: "localhost" port: 5432 name: "tuskapp" user: "postgres" password: "secret" # Hard-coded password!

[features] cache_enabled: true ml_enabled: false debug_mode: true

2. Environment-Specific Configuration

Good: Environment-aware configuration

$environment: @env("APP_ENV", "development")

[server] host: @if($environment == "production", "0.0.0.0", "localhost") port: @if($environment == "production", 80, 8080) ssl: @if($environment == "production", true, false)

[logging] level: @if($environment == "production", "error", "debug") format: @if($environment == "production", "json", "text") file: @if($environment == "production", "/var/log/app.log", "console")

[security] ssl_required: @if($environment == "production", true, false) cors_origins: @if($environment == "production", ["https://myapp.com"], ["*"])

3. Security Best Practices

Good: Secure configuration practices

[security]

Use secure environment variables for sensitive data

database_password: @env.secure("DB_PASSWORD") api_key: @env.secure("API_KEY") jwt_secret: @env.secure("JWT_SECRET")

Encrypt sensitive configuration values

connection_string: @encrypt("${user}:${password}@${host}", "AES-256-GCM") session_secret: @encrypt($session_secret_raw, "ChaCha20-Poly1305")

Validate required configuration

required_fields: @validate.required(["api_key", "database_password", "jwt_secret"])
// Good: Secure configuration service
public class SecureConfigurationService
{
    private readonly TuskLang _parser;
    private readonly IEncryptionProvider _encryptionProvider;
    private readonly IValidationProvider _validationProvider;
    
    public SecureConfigurationService(string masterKey)
    {
        _parser = new TuskLang();
        _encryptionProvider = new TuskEncryptionProvider(masterKey);
        _validationProvider = new TuskValidationProvider();
        
        _parser.SetEncryptionProvider(_encryptionProvider);
        _parser.SetValidationProvider(_validationProvider);
    }
    
    public async Task<Dictionary<string, object>> GetSecureConfigurationAsync(string filePath)
    {
        // Validate configuration before parsing
        if (!_parser.Validate(filePath))
        {
            throw new InvalidOperationException("Configuration validation failed");
        }
        
        // Parse configuration
        var config = _parser.ParseFile(filePath);
        
        // Validate required fields
        var requiredFields = config["security"]["required_fields"] as List<string>;
        foreach (var field in requiredFields)
        {
            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(field)))
            {
                throw new InvalidOperationException($"Required environment variable {field} is not set");
            }
        }
        
        return config;
    }
}

4. Performance Optimization

Good: Performance-optimized configuration

[performance]

Cache expensive operations

expensive_query: @cache("5m", @query("SELECT COUNT(*) FROM large_table")) api_response: @cache("30s", @http("GET", "https://api.example.com/data"))

Adaptive caching based on load

adaptive_cache: @cache(@if(@metrics("cpu_usage", 0) > 80, "30s", "5m"), @query("SELECT * FROM heavy_table"))

Batch operations for efficiency

batch_queries: @query.batch([ "SELECT COUNT(*) FROM users", "SELECT COUNT(*) FROM orders", "SELECT COUNT(*) FROM products" ])

Connection pooling

database_pool { max_open_conns: @if($environment == "production", 100, 20) max_idle_conns: @if($environment == "production", 50, 10) conn_max_lifetime: "5m" }
// Good: Performance-optimized service
public class OptimizedConfigurationService
{
    private readonly TuskLang _parser;
    private readonly ICacheProvider _cacheProvider;
    private readonly IDatabaseAdapter _databaseAdapter;
    
    public OptimizedConfigurationService()
    {
        _parser = new TuskLang();
        
        // Multi-layer caching
        _cacheProvider = new MultiLayerCacheProvider(new ICacheProvider[]
        {
            new MemoryCacheProvider(),           // L1: In-memory cache
            new RedisCacheProvider(new RedisConfig
            {
                Host = "localhost",
                Port = 6379
            }),                                  // L2: Redis cache
            new DatabaseCacheProvider()          // L3: Database cache
        });
        
        // Optimized database adapter
        _databaseAdapter = new PostgreSQLAdapter(new PostgreSQLConfig
        {
            Host = "localhost",
            Database = "myapp"
        }, new PoolConfig
        {
            MaxOpenConns = 100,
            MaxIdleConns = 50,
            ConnMaxLifetime = 300000,
            ConnMaxIdleTime = 60000
        });
        
        _parser.SetCacheProvider(_cacheProvider);
        _parser.SetDatabaseAdapter(_databaseAdapter);
    }
    
    public async Task<Dictionary<string, object>> GetOptimizedConfigurationAsync(string filePath)
    {
        // Preload frequently accessed data
        await PreloadCacheAsync();
        
        // Parse with optimizations
        return _parser.ParseFile(filePath);
    }
    
    private async Task PreloadCacheAsync()
    {
        var preloadTasks = new[]
        {
            PreloadUserCountAsync(),
            PreloadActiveUsersAsync(),
            PreloadSystemMetricsAsync()
        };
        
        await Task.WhenAll(preloadTasks);
    }
}

🔧 Code Quality Best Practices

1. Error Handling

// Good: Comprehensive error handling
public class RobustConfigurationService
{
    private readonly TuskLang _parser;
    private readonly ILogger<RobustConfigurationService> _logger;
    
    public RobustConfigurationService(ILogger<RobustConfigurationService> logger)
    {
        _parser = new TuskLang();
        _logger = logger;
    }
    
    public async Task<Dictionary<string, object>> GetConfigurationAsync(string filePath)
    {
        try
        {
            // Validate file exists
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException($"Configuration file not found: {filePath}");
            }
            
            // Validate syntax
            if (!_parser.Validate(filePath))
            {
                var validationResult = _parser.ValidateWithDetails(filePath);
                var errors = string.Join(", ", validationResult.Errors.Select(e => $"{e.Message} at line {e.Line}"));
                throw new InvalidOperationException($"Configuration validation failed: {errors}");
            }
            
            // Parse configuration
            var config = _parser.ParseFile(filePath);
            
            // Validate required sections
            ValidateRequiredSections(config);
            
            _logger.LogInformation("Configuration loaded successfully from {FilePath}", filePath);
            return config;
        }
        catch (TuskLangParseException ex)
        {
            _logger.LogError(ex, "Configuration parsing failed: {Message}", ex.Message);
            return GetFallbackConfiguration();
        }
        catch (DatabaseConnectionException ex)
        {
            _logger.LogError(ex, "Database connection failed: {Message}", ex.Message);
            return GetFallbackConfiguration();
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Unexpected error loading configuration: {Message}", ex.Message);
            return GetFallbackConfiguration();
        }
    }
    
    private void ValidateRequiredSections(Dictionary<string, object> config)
    {
        var requiredSections = new[] { "app", "database", "security" };
        
        foreach (var section in requiredSections)
        {
            if (!config.ContainsKey(section))
            {
                throw new InvalidOperationException($"Required configuration section '{section}' is missing");
            }
        }
    }
    
    private Dictionary<string, object> GetFallbackConfiguration()
    {
        _logger.LogWarning("Using fallback configuration");
        
        return new Dictionary<string, object>
        {
            ["app"] = new Dictionary<string, object>
            {
                ["name"] = "TuskLangApp",
                ["environment"] = "fallback",
                ["fallback_mode"] = true
            },
            ["database"] = new Dictionary<string, object>
            {
                ["host"] = "localhost",
                ["port"] = 5432,
                ["name"] = "tuskapp"
            },
            ["security"] = new Dictionary<string, object>
            {
                ["ssl_required"] = false,
                ["debug_mode"] = true
            }
        };
    }
}

2. Logging and Monitoring

// Good: Comprehensive logging and monitoring
public class MonitoredConfigurationService
{
    private readonly TuskLang _parser;
    private readonly ILogger<MonitoredConfigurationService> _logger;
    private readonly IMetricsCollector _metricsCollector;
    private readonly ITracer _tracer;
    
    public MonitoredConfigurationService(
        ILogger<MonitoredConfigurationService> logger,
        IMetricsCollector metricsCollector,
        ITracer tracer)
    {
        _parser = new TuskLang();
        _logger = logger;
        _metricsCollector = metricsCollector;
        _tracer = tracer;
    }
    
    public async Task<Dictionary<string, object>> GetMonitoredConfigurationAsync(string filePath)
    {
        using var span = _tracer.StartSpan("configuration.parse");
        span.SetTag("file_path", filePath);
        
        var stopwatch = Stopwatch.StartNew();
        
        try
        {
            _logger.LogInformation("Starting configuration parse for {FilePath}", filePath);
            
            // Parse configuration
            var config = _parser.ParseFile(filePath);
            
            stopwatch.Stop();
            
            // Record metrics
            await _metricsCollector.RecordAsync("configuration_parse_duration_ms", stopwatch.ElapsedMilliseconds);
            await _metricsCollector.RecordAsync("configuration_parse_success", 1);
            
            // Log success
            _logger.LogInformation("Configuration parsed successfully in {Duration}ms", stopwatch.ElapsedMilliseconds);
            
            span.SetTag("success", true);
            span.SetTag("duration_ms", stopwatch.ElapsedMilliseconds);
            
            return config;
        }
        catch (Exception ex)
        {
            stopwatch.Stop();
            
            // Record error metrics
            await _metricsCollector.RecordAsync("configuration_parse_error", 1);
            await _metricsCollector.RecordAsync("configuration_parse_duration_ms", stopwatch.ElapsedMilliseconds);
            
            // Log error
            _logger.LogError(ex, "Configuration parse failed after {Duration}ms", stopwatch.ElapsedMilliseconds);
            
            span.SetTag("success", false);
            span.SetTag("error", ex.Message);
            span.SetTag("duration_ms", stopwatch.ElapsedMilliseconds);
            
            throw;
        }
    }
}

3. Testing Best Practices

// Good: Comprehensive testing
[TestFixture]
public class ConfigurationServiceTests
{
    private TuskLang _parser;
    private IConfigurationService _configurationService;
    
    [SetUp]
    public void Setup()
    {
        _parser = new TuskLang();
        _configurationService = new ConfigurationService(_parser);
    }
    
    [Test]
    public void TestBasicConfigurationParsing()
    {
        // Arrange
        var tskContent = @"
[app]
name: ""TestApp""
version: ""1.0.0""
environment: ""test""
";
        
        // Act
        var config = _parser.Parse(tskContent);
        
        // Assert
        Assert.That(config, Is.Not.Null);
        Assert.That(config["app"]["name"], Is.EqualTo("TestApp"));
        Assert.That(config["app"]["version"], Is.EqualTo("1.0.0"));
        Assert.That(config["app"]["environment"], Is.EqualTo("test"));
    }
    
    [Test]
    public void TestEnvironmentVariableSubstitution()
    {
        // Arrange
        Environment.SetEnvironmentVariable("TEST_VAR", "test_value");
        var tskContent = @"
[test]
value: @env(""TEST_VAR"", ""default"")
";
        
        // Act
        var config = _parser.Parse(tskContent);
        
        // Assert
        Assert.That(config["test"]["value"], Is.EqualTo("test_value"));
    }
    
    [Test]
    public void TestDatabaseQueryIntegration()
    {
        // Arrange
        var sqliteAdapter = new SQLiteAdapter(":memory:");
        _parser.SetDatabaseAdapter(sqliteAdapter);
        
        // Create test data
        sqliteAdapter.Execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
        sqliteAdapter.Execute("INSERT INTO users (name) VALUES ('Alice'), ('Bob')");
        
        var tskContent = @"
[stats]
user_count: @query(""SELECT COUNT(*) FROM users"")
";
        
        // Act
        var config = _parser.Parse(tskContent);
        
        // Assert
        Assert.That(config["stats"]["user_count"], Is.EqualTo(2));
    }
    
    [Test]
    public void TestConditionalLogic()
    {
        // Arrange
        Environment.SetEnvironmentVariable("APP_ENV", "production");
        var tskContent = @"
$environment: @env(""APP_ENV"", ""development"")
[server]
host: @if($environment == ""production"", ""0.0.0.0"", ""localhost"")
port: @if($environment == ""production"", 80, 8080)
";
        
        // Act
        var config = _parser.Parse(tskContent);
        
        // Assert
        Assert.That(config["server"]["host"], Is.EqualTo("0.0.0.0"));
        Assert.That(config["server"]["port"], Is.EqualTo(80));
    }
    
    [Test]
    public void TestErrorHandling()
    {
        // Arrange
        var invalidTskContent = @"
[app]
name: ""TestApp""
[invalid_section
value: ""test""
";
        
        // Act & Assert
        Assert.Throws<TuskLangParseException>(() => _parser.Parse(invalidTskContent));
    }
    
    [Test]
    public void TestConfigurationValidation()
    {
        // Arrange
        var validTskContent = @"
[app]
name: ""TestApp""
version: ""1.0.0""
";
        
        var invalidTskContent = @"
[app]
name: ""TestApp""
[invalid_section
";
        
        // Act & Assert
        Assert.That(_parser.Validate(validTskContent), Is.True);
        Assert.That(_parser.Validate(invalidTskContent), Is.False);
    }
    
    [Test]
    public async Task TestCachingIntegration()
    {
        // Arrange
        var cacheProvider = new MockCacheProvider();
        _parser.SetCacheProvider(cacheProvider);
        
        var tskContent = @"
[performance]
cached_value: @cache(""5m"", ""expensive_operation"")
";
        
        // Act
        var config = _parser.Parse(tskContent);
        
        // Assert
        Assert.That(config["performance"]["cached_value"], Is.EqualTo("expensive_operation"));
        Assert.That(cacheProvider.WasCalled, Is.True);
    }
}

🔒 Security Best Practices

1. Secrets Management

// Good: Secure secrets management
public class SecureSecretsService
{
    private readonly IKeyVaultClient _keyVaultClient;
    private readonly ILogger<SecureSecretsService> _logger;
    
    public SecureSecretsService(IKeyVaultClient keyVaultClient, ILogger<SecureSecretsService> logger)
    {
        _keyVaultClient = keyVaultClient;
        _logger = logger;
    }
    
    public async Task<Dictionary<string, string>> GetSecretsAsync(string[] secretNames)
    {
        var secrets = new Dictionary<string, string>();
        
        foreach (var secretName in secretNames)
        {
            try
            {
                var secret = await _keyVaultClient.GetSecretAsync(secretName);
                secrets[secretName] = secret.Value;
                
                _logger.LogDebug("Retrieved secret: {SecretName}", secretName);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to retrieve secret: {SecretName}", secretName);
                throw;
            }
        }
        
        return secrets;
    }
    
    public async Task SetSecretAsync(string secretName, string secretValue)
    {
        try
        {
            await _keyVaultClient.SetSecretAsync(secretName, secretValue);
            _logger.LogInformation("Secret set successfully: {SecretName}", secretName);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to set secret: {SecretName}", secretName);
            throw;
        }
    }
}

2. Input Validation

// Good: Comprehensive input validation
public class ConfigurationValidator
{
    private readonly IValidationProvider _validationProvider;
    
    public ConfigurationValidator(IValidationProvider validationProvider)
    {
        _validationProvider = validationProvider;
    }
    
    public ValidationResult ValidateConfiguration(Dictionary<string, object> config)
    {
        var errors = new List<string>();
        
        // Validate required sections
        var requiredSections = new[] { "app", "database", "security" };
        foreach (var section in requiredSections)
        {
            if (!config.ContainsKey(section))
            {
                errors.Add($"Required section '{section}' is missing");
            }
        }
        
        // Validate app section
        if (config.ContainsKey("app"))
        {
            var app = config["app"] as Dictionary<string, object>;
            if (app != null)
            {
                if (!app.ContainsKey("name") || string.IsNullOrEmpty(app["name"]?.ToString()))
                {
                    errors.Add("App name is required");
                }
                
                if (!app.ContainsKey("version") || string.IsNullOrEmpty(app["version"]?.ToString()))
                {
                    errors.Add("App version is required");
                }
            }
        }
        
        // Validate database section
        if (config.ContainsKey("database"))
        {
            var database = config["database"] as Dictionary<string, object>;
            if (database != null)
            {
                if (!database.ContainsKey("host") || string.IsNullOrEmpty(database["host"]?.ToString()))
                {
                    errors.Add("Database host is required");
                }
                
                if (!database.ContainsKey("port") || !int.TryParse(database["port"]?.ToString(), out _))
                {
                    errors.Add("Database port must be a valid integer");
                }
            }
        }
        
        // Validate security section
        if (config.ContainsKey("security"))
        {
            var security = config["security"] as Dictionary<string, object>;
            if (security != null)
            {
                if (security.ContainsKey("ssl_required") && !bool.TryParse(security["ssl_required"]?.ToString(), out _))
                {
                    errors.Add("SSL required must be a valid boolean");
                }
            }
        }
        
        return new ValidationResult
        {
            IsValid = errors.Count == 0,
            Errors = errors
        };
    }
}

public class ValidationResult { public bool IsValid { get; set; } public List<string> Errors { get; set; } = new List<string>(); }

📊 Performance Best Practices

1. Caching Strategies

// Good: Intelligent caching strategies
public class IntelligentCacheService
{
    private readonly ICacheProvider _cacheProvider;
    private readonly IMetricsCollector _metricsCollector;
    
    public IntelligentCacheService(ICacheProvider cacheProvider, IMetricsCollector metricsCollector)
    {
        _cacheProvider = cacheProvider;
        _metricsCollector = metricsCollector;
    }
    
    public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> factory, TimeSpan? ttl = null)
    {
        try
        {
            // Try to get from cache
            var cached = await _cacheProvider.GetAsync(key);
            if (cached != null)
            {
                await _metricsCollector.RecordAsync("cache_hit", 1);
                return (T)cached;
            }
            
            // Cache miss - execute factory
            await _metricsCollector.RecordAsync("cache_miss", 1);
            var value = await factory();
            
            // Store in cache with adaptive TTL
            var cacheTtl = ttl ?? GetAdaptiveTtl(key);
            await _cacheProvider.SetAsync(key, value, cacheTtl);
            
            return value;
        }
        catch (Exception ex)
        {
            await _metricsCollector.RecordAsync("cache_error", 1);
            throw;
        }
    }
    
    private TimeSpan GetAdaptiveTtl(string key)
    {
        // Adaptive TTL based on key type and system load
        return key switch
        {
            var k when k.Contains("user") => TimeSpan.FromMinutes(5),
            var k when k.Contains("product") => TimeSpan.FromMinutes(30),
            var k when k.Contains("analytics") => TimeSpan.FromHours(1),
            _ => TimeSpan.FromMinutes(10)
        };
    }
}

2. Database Optimization

// Good: Database optimization strategies
public class OptimizedDatabaseService
{
    private readonly IDatabaseAdapter _adapter;
    private readonly ILogger<OptimizedDatabaseService> _logger;
    
    public OptimizedDatabaseService(IDatabaseAdapter adapter, ILogger<OptimizedDatabaseService> logger)
    {
        _adapter = adapter;
        _logger = logger;
    }
    
    public async Task<List<Dictionary<string, object>>> GetOptimizedQueryAsync(string query, object[] parameters = null)
    {
        var stopwatch = Stopwatch.StartNew();
        
        try
        {
            // Use prepared statements for repeated queries
            var preparedQuery = await _adapter.PrepareAsync(query);
            var result = await preparedQuery.ExecuteAsync(parameters ?? new object[0]);
            
            stopwatch.Stop();
            
            if (stopwatch.ElapsedMilliseconds > 1000)
            {
                _logger.LogWarning("Slow query detected: {Query} took {Duration}ms", query, stopwatch.ElapsedMilliseconds);
            }
            
            return result;
        }
        catch (Exception ex)
        {
            stopwatch.Stop();
            _logger.LogError(ex, "Query failed: {Query} after {Duration}ms", query, stopwatch.ElapsedMilliseconds);
            throw;
        }
    }
    
    public async Task<Dictionary<string, object>> GetBatchQueriesAsync(string[] queries)
    {
        // Execute multiple queries in parallel
        var tasks = queries.Select(q => GetOptimizedQueryAsync(q));
        var results = await Task.WhenAll(tasks);
        
        var batchResult = new Dictionary<string, object>();
        for (int i = 0; i < queries.Length; i++)
        {
            batchResult[queries[i]] = results[i];
        }
        
        return batchResult;
    }
}

🔄 Operational Best Practices

1. Configuration Management

// Good: Configuration management service
public class ConfigurationManagementService
{
    private readonly TuskLang _parser;
    private readonly ILogger<ConfigurationManagementService> _logger;
    private readonly IConfigurationValidator _validator;
    
    public ConfigurationManagementService(
        TuskLang parser,
        ILogger<ConfigurationManagementService> logger,
        IConfigurationValidator validator)
    {
        _parser = parser;
        _logger = logger;
        _validator = validator;
    }
    
    public async Task<Dictionary<string, object>> LoadConfigurationAsync(string filePath)
    {
        _logger.LogInformation("Loading configuration from {FilePath}", filePath);
        
        // Validate file exists
        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException($"Configuration file not found: {filePath}");
        }
        
        // Parse configuration
        var config = _parser.ParseFile(filePath);
        
        // Validate configuration
        var validationResult = _validator.ValidateConfiguration(config);
        if (!validationResult.IsValid)
        {
            var errors = string.Join(", ", validationResult.Errors);
            throw new InvalidOperationException($"Configuration validation failed: {errors}");
        }
        
        _logger.LogInformation("Configuration loaded successfully");
        return config;
    }
    
    public async Task<bool> ValidateConfigurationAsync(string filePath)
    {
        try
        {
            var config = await LoadConfigurationAsync(filePath);
            return config != null && config.Count > 0;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Configuration validation failed");
            return false;
        }
    }
    
    public async Task<Dictionary<string, object>> ReloadConfigurationAsync(string filePath)
    {
        _logger.LogInformation("Reloading configuration from {FilePath}", filePath);
        
        // Clear any cached configuration
        // This would depend on your caching implementation
        
        // Load new configuration
        return await LoadConfigurationAsync(filePath);
    }
}

2. Health Monitoring

// Good: Health monitoring service
public class HealthMonitoringService
{
    private readonly IHealthCheckService _healthCheckService;
    private readonly ILogger<HealthMonitoringService> _logger;
    private readonly IMetricsCollector _metricsCollector;
    
    public HealthMonitoringService(
        IHealthCheckService healthCheckService,
        ILogger<HealthMonitoringService> logger,
        IMetricsCollector metricsCollector)
    {
        _healthCheckService = healthCheckService;
        _logger = logger;
        _metricsCollector = metricsCollector;
    }
    
    public async Task<HealthReport> GetHealthReportAsync()
    {
        var report = await _healthCheckService.CheckHealthAsync();
        
        // Record health metrics
        await _metricsCollector.RecordAsync("health_status", report.Status == HealthStatus.Healthy ? 1 : 0);
        
        foreach (var entry in report.Entries)
        {
            await _metricsCollector.RecordAsync($"health_check_{entry.Key}", 
                entry.Value.Status == HealthStatus.Healthy ? 1 : 0);
        }
        
        // Log health status
        if (report.Status != HealthStatus.Healthy)
        {
            var unhealthyChecks = report.Entries
                .Where(e => e.Value.Status != HealthStatus.Healthy)
                .Select(e => e.Key);
            
            _logger.LogWarning("Health check failed for: {UnhealthyChecks}", 
                string.Join(", ", unhealthyChecks));
        }
        
        return report;
    }
    
    public async Task StartHealthMonitoringAsync()
    {
        // Monitor health every 30 seconds
        _ = Task.Run(async () =>
        {
            while (true)
            {
                try
                {
                    await GetHealthReportAsync();
                    await Task.Delay(TimeSpan.FromSeconds(30));
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Health monitoring failed");
                    await Task.Delay(TimeSpan.FromSeconds(60));
                }
            }
        });
    }
}

🎯 Best Practices Summary

1. Configuration Structure

- ✅ Organize logically - Group related settings together - ✅ Use global variables - For reusability and consistency - ✅ Environment awareness - Adapt to different environments - ✅ Document thoroughly - Comments and descriptions

2. Security

- ✅ Use secure environment variables - Never hard-code secrets - ✅ Encrypt sensitive data - Use @encrypt for sensitive values - ✅ Validate inputs - Check all configuration values - ✅ Access control - Limit who can modify configuration

3. Performance

- ✅ Cache intelligently - Use appropriate TTLs and strategies - ✅ Optimize queries - Use efficient database queries - ✅ Monitor performance - Track metrics and optimize - ✅ Scale appropriately - Handle increased load

4. Operations

- ✅ Comprehensive logging - Log all important events - ✅ Health monitoring - Monitor system health - ✅ Error handling - Graceful degradation and fallbacks - ✅ Testing - Comprehensive test coverage

5. Code Quality

- ✅ Clean code - Readable and maintainable - ✅ Error handling - Robust error handling - ✅ Documentation - Clear and comprehensive - ✅ Consistency - Follow established patterns

🎉 You're Ready!

You've mastered the best practices for TuskLang in production! You can now:

- ✅ Write clean configurations - Well-organized and maintainable - ✅ Implement security - Protect sensitive data and prevent vulnerabilities - ✅ Optimize performance - Efficient and scalable configurations - ✅ Monitor operations - Comprehensive logging and health monitoring - ✅ Handle errors gracefully - Robust error handling and fallbacks - ✅ Test thoroughly - Comprehensive testing strategies

🔥 What's Next?

Ready to troubleshoot and scale? Explore:

1. Troubleshooting - Common issues and solutions 2. Scaling Strategies - Handle massive scale 3. Advanced Patterns - Complex use cases

---

"We don't bow to any king" - Your best practices, your excellence, your success.

Build production-ready configurations with confidence! 🚀