Disable Logging Configuration
There are several ways to disable or reduce logging in ByteHide Logger, from complete shutdown to selective filtering. Choose the method that best fits your needs.
Complete Logging Disable
Method 1: Using Log.Disable() (Recommended)
Use the built-in disable method to stop all logging:
C#
// Initialize logger normally
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Info,
ConsoleEnabled = true
});
// Disable logging completely
Log.Disable();
// All logging calls will be ignored
Log.Info("This will not be logged");
Log.Error("This will also be ignored");
// Re-enable logging when needed
Log.Enable();
Log.Info("This will be logged again");// Initialize logger normally
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Info,
ConsoleEnabled = true
});
// Disable logging completely
Log.Disable();
// All logging calls will be ignored
Log.Info("This will not be logged");
Log.Error("This will also be ignored");
// Re-enable logging when needed
Log.Enable();
Log.Info("This will be logged again");Method 2: Don't Initialize Logger
The simplest way to disable logging is to not initialize the logger:
C#
// Don't call Log.Initialize() at all
// Log.Initialize(new LogSettings { ... }); // Commented out
// All logging calls will be ignored
Log.Info("This will not be logged");
Log.Error("This will also be ignored");// Don't call Log.Initialize() at all
// Log.Initialize(new LogSettings { ... }); // Commented out
// All logging calls will be ignored
Log.Info("This will not be logged");
Log.Error("This will also be ignored");Method 3: Set Minimum Level to Highest
C#
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Critical // Only critical messages
});
// These will be ignored
Log.Info("Ignored");
Log.Warn("Ignored");
Log.Error("Ignored");
// Only this will be logged
Log.Critical("This will be logged");Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Critical // Only critical messages
});
// These will be ignored
Log.Info("Ignored");
Log.Warn("Ignored");
Log.Error("Ignored");
// Only this will be logged
Log.Critical("This will be logged");Conditional Disabling
Environment-Based Disabling
C#
var isProduction = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production";
var isHighTraffic = Environment.GetEnvironmentVariable("HIGH_TRAFFIC_MODE") == "true";
if (!isProduction || isHighTraffic)
{
// Don't initialize logger in production or high-traffic scenarios
return;
}
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Warn
});var isProduction = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production";
var isHighTraffic = Environment.GetEnvironmentVariable("HIGH_TRAFFIC_MODE") == "true";
if (!isProduction || isHighTraffic)
{
// Don't initialize logger in production or high-traffic scenarios
return;
}
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Warn
});Feature Flag Disabling
C#
public static class LoggerConfig
{
public static void DisableLogging()
{
Log.Disable();
}
public static void EnableLogging()
{
Log.Enable();
}
public static void InitializeWithFeatureFlag(bool loggingEnabled)
{
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Info
});
if (!loggingEnabled)
{
Log.Disable();
}
}
}public static class LoggerConfig
{
public static void DisableLogging()
{
Log.Disable();
}
public static void EnableLogging()
{
Log.Enable();
}
public static void InitializeWithFeatureFlag(bool loggingEnabled)
{
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Info
});
if (!loggingEnabled)
{
Log.Disable();
}
}
}Selective Disabling
Disable Console Only
C#
Log.Initialize(new LogSettings
{
ConsoleEnabled = false, // Disable console output
Persist = true, // Keep file logging
FilePath = "logs/app.log"
});Log.Initialize(new LogSettings
{
ConsoleEnabled = false, // Disable console output
Persist = true, // Keep file logging
FilePath = "logs/app.log"
});Disable File Logging Only
C#
Log.Initialize(new LogSettings
{
Persist = false, // Disable file logging
ConsoleEnabled = true // Keep console output
});Log.Initialize(new LogSettings
{
Persist = false, // Disable file logging
ConsoleEnabled = true // Keep console output
});Disable Cloud Logging Only
C#
Log.Initialize(new LogSettings
{
// Configure local logging
Persist = true,
ConsoleEnabled = true
});
// Don't set project token - logs stay local only
// Log.SetProjectToken("token"); // Commented outLog.Initialize(new LogSettings
{
// Configure local logging
Persist = true,
ConsoleEnabled = true
});
// Don't set project token - logs stay local only
// Log.SetProjectToken("token"); // Commented outPerformance-Based Disabling
High-Traffic Configuration
C#
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Error, // Only errors
ConsoleEnabled = false, // No console overhead
IncludeCallerInfo = false, // No caller info overhead
DuplicateSuppressionWindow = TimeSpan.FromMinutes(5) // Aggressive suppression
});Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Error, // Only errors
ConsoleEnabled = false, // No console overhead
IncludeCallerInfo = false, // No caller info overhead
DuplicateSuppressionWindow = TimeSpan.FromMinutes(5) // Aggressive suppression
});Minimal Logging Configuration
C#
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Critical, // Only critical messages
ConsoleEnabled = false,
IncludeCallerInfo = false,
Persist = true,
FilePath = "logs/critical-only.log"
});Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Critical, // Only critical messages
ConsoleEnabled = false,
IncludeCallerInfo = false,
Persist = true,
FilePath = "logs/critical-only.log"
});Runtime Disabling
Using Built-in Enable/Disable Methods
C#
public static class RuntimeLogControl
{
public static void DisableAllLogging()
{
// Use built-in disable method
Log.Disable();
}
public static void EnableLogging()
{
// Use built-in enable method
Log.Enable();
}
public static void ToggleLogging(bool enabled)
{
if (enabled)
Log.Enable();
else
Log.Disable();
}
}public static class RuntimeLogControl
{
public static void DisableAllLogging()
{
// Use built-in disable method
Log.Disable();
}
public static void EnableLogging()
{
// Use built-in enable method
Log.Enable();
}
public static void ToggleLogging(bool enabled)
{
if (enabled)
Log.Enable();
else
Log.Disable();
}
}Dynamic Level Adjustment
C#
public static class RuntimeLogControl
{
public static void EnableErrorsOnly()
{
Log.Enable(); // Ensure logging is enabled first
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Error
});
}
public static void EnableFullLogging()
{
Log.Enable(); // Ensure logging is enabled first
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Debug,
ConsoleEnabled = true,
Persist = true
});
}
}public static class RuntimeLogControl
{
public static void EnableErrorsOnly()
{
Log.Enable(); // Ensure logging is enabled first
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Error
});
}
public static void EnableFullLogging()
{
Log.Enable(); // Ensure logging is enabled first
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Debug,
ConsoleEnabled = true,
Persist = true
});
}
}Configuration Examples
Development - Full Logging
C#
#if DEBUG
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Debug,
ConsoleEnabled = true,
IncludeCallerInfo = true,
Persist = true
});
#endif#if DEBUG
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Debug,
ConsoleEnabled = true,
IncludeCallerInfo = true,
Persist = true
});
#endifProduction - Minimal Logging
C#
#if !DEBUG
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Error, // Errors only
ConsoleEnabled = false, // No console
IncludeCallerInfo = false, // No caller info
Persist = true,
FilePath = "logs/errors-only.log"
});
#endif#if !DEBUG
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Error, // Errors only
ConsoleEnabled = false, // No console
IncludeCallerInfo = false, // No caller info
Persist = true,
FilePath = "logs/errors-only.log"
});
#endifTesting - Disabled Logging
C#
public class TestConfiguration
{
public static void DisableLoggingForTests()
{
// Don't initialize logger during tests
// This prevents log files from being created during test runs
}
public static void EnableTestLogging()
{
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Error, // Only test errors
ConsoleEnabled = true, // Show in test output
Persist = false // Don't create files
});
}
}public class TestConfiguration
{
public static void DisableLoggingForTests()
{
// Don't initialize logger during tests
// This prevents log files from being created during test runs
}
public static void EnableTestLogging()
{
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Error, // Only test errors
ConsoleEnabled = true, // Show in test output
Persist = false // Don't create files
});
}
}Environment Variable Control
Complete Disable via Environment
Bash
# Set environment variable to disable logging
BYTEHIDE_LOGGING_DISABLED=true# Set environment variable to disable logging
BYTEHIDE_LOGGING_DISABLED=trueC#
public static void InitializeWithEnvironmentControl()
{
var loggingDisabled = Environment.GetEnvironmentVariable("BYTEHIDE_LOGGING_DISABLED") == "true";
if (loggingDisabled)
{
// Don't initialize logger
return;
}
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Info
});
}public static void InitializeWithEnvironmentControl()
{
var loggingDisabled = Environment.GetEnvironmentVariable("BYTEHIDE_LOGGING_DISABLED") == "true";
if (loggingDisabled)
{
// Don't initialize logger
return;
}
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Info
});
}Level Control via Environment
Bash
# Set to disable most logging
BYTEHIDE_LOG_LEVEL=Critical# Set to disable most logging
BYTEHIDE_LOG_LEVEL=CriticalC#
var logLevel = Environment.GetEnvironmentVariable("BYTEHIDE_LOG_LEVEL") ?? "Warn";
var level = Enum.Parse<LogLevel>(logLevel);
Log.Initialize(new LogSettings
{
MinimumLevel = level
});var logLevel = Environment.GetEnvironmentVariable("BYTEHIDE_LOG_LEVEL") ?? "Warn";
var level = Enum.Parse<LogLevel>(logLevel);
Log.Initialize(new LogSettings
{
MinimumLevel = level
});Best Practices
Disabling Logging Best Practices
- Use environment variables for flexible logging control
- Consider performance impact when choosing disable methods
- Keep critical error logging even in high-performance scenarios
- Test logging disable/enable in your deployment pipeline
- Document logging configuration for your operations team
Verification
Check if Logging is Active
C#
public static class LoggingStatus
{
public static void TestLoggingStatus()
{
// Test with different methods
Log.Info("Testing if logging is active");
// Disable and test
Log.Disable();
Log.Info("This should not appear");
// Re-enable and test
Log.Enable();
Log.Info("Logging re-enabled successfully");
}
public static void ReportLoggingMethods()
{
Console.WriteLine("Available logging control methods:");
Console.WriteLine("- Log.Enable() - Enables logging");
Console.WriteLine("- Log.Disable() - Disables logging");
}
}public static class LoggingStatus
{
public static void TestLoggingStatus()
{
// Test with different methods
Log.Info("Testing if logging is active");
// Disable and test
Log.Disable();
Log.Info("This should not appear");
// Re-enable and test
Log.Enable();
Log.Info("Logging re-enabled successfully");
}
public static void ReportLoggingMethods()
{
Console.WriteLine("Available logging control methods:");
Console.WriteLine("- Log.Enable() - Enables logging");
Console.WriteLine("- Log.Disable() - Disables logging");
}
}