using Common.Configuration.General;
using Infrastructure.Configuration;
using Microsoft.Extensions.Logging;
using Serilog.Core;
using Serilog.Events;
namespace Infrastructure.Logging;
///
/// Manages logging configuration and provides dynamic log level control
///
public class LoggingConfigManager
{
private readonly IConfigManager _configManager;
private readonly LoggingLevelSwitch _levelSwitch;
private readonly ILogger _logger;
public LoggingConfigManager(IConfigManager configManager, ILogger logger)
{
_configManager = configManager;
_logger = logger;
// Initialize with default level
_levelSwitch = new LoggingLevelSwitch();
// Load settings from configuration
LoadConfiguration();
}
///
/// Gets the level switch used to dynamically control log levels
///
public LoggingLevelSwitch GetLevelSwitch() => _levelSwitch;
///
/// Updates the global log level and persists the change to configuration
///
/// The new log level
public async Task SetLogLevel(LogEventLevel level)
{
// Change the level in the switch
_levelSwitch.MinimumLevel = level;
_logger.LogInformation("Setting global log level to {level}", level);
// Update configuration
try
{
var config = await _configManager.GetConfigurationAsync();
config.LogLevel = level;
// TODO use SetProp
await _configManager.SaveConfigurationAsync(config);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to update log level in configuration");
}
}
///
/// Gets the current global log level
///
public LogEventLevel GetLogLevel() => _levelSwitch.MinimumLevel;
///
/// Loads logging settings from configuration
///
private void LoadConfiguration()
{
try
{
var config = _configManager.GetConfiguration();
_levelSwitch.MinimumLevel = config.LogLevel;
}
catch (Exception ex)
{
// Just log and continue with defaults
_logger.LogError(ex, "Failed to load logging configuration, using defaults");
}
}
}