This commit is contained in:
Flaminel
2025-05-19 15:32:57 +03:00
parent a1bd278652
commit eb0f782f53
8 changed files with 206 additions and 50 deletions

View File

@@ -0,0 +1,76 @@
using Infrastructure.Logging;
using Microsoft.AspNetCore.Mvc;
using Serilog.Events;
namespace Executable.Controllers;
[ApiController]
[Route("api/[controller]")]
public class LoggingController : ControllerBase
{
private readonly LoggingConfigManager _loggingConfigManager;
private readonly ILogger<LoggingController> _logger;
public LoggingController(LoggingConfigManager loggingConfigManager, ILogger<LoggingController> logger)
{
_loggingConfigManager = loggingConfigManager;
_logger = logger;
}
/// <summary>
/// Gets the current global log level
/// </summary>
/// <returns>Current log level</returns>
[HttpGet("level")]
public IActionResult GetLogLevel()
{
return Ok(new { level = _loggingConfigManager.GetLogLevel().ToString() });
}
/// <summary>
/// Sets the global log level
/// </summary>
/// <param name="request">Log level request containing the new level</param>
/// <returns>Result with the new log level</returns>
[HttpPut("level")]
public async Task<IActionResult> SetLogLevel([FromBody] LogLevelRequest request)
{
if (!Enum.TryParse<LogEventLevel>(request.Level, true, out var logLevel))
{
return BadRequest(new
{
error = "Invalid log level",
validLevels = Enum.GetNames<LogEventLevel>()
});
}
await _loggingConfigManager.SetLogLevel(logLevel);
// Log at the new level to confirm it's working
_logger.WithCategory(LoggingCategoryConstants.System)
.LogInformation("Log level changed to {Level}", logLevel);
return Ok(new { level = logLevel.ToString() });
}
/// <summary>
/// Get a list of valid log levels
/// </summary>
/// <returns>All valid log level values</returns>
[HttpGet("levels")]
public IActionResult GetValidLogLevels()
{
return Ok(new
{
levels = Enum.GetNames<LogEventLevel>()
});
}
}
/// <summary>
/// Request model for changing log level
/// </summary>
public class LogLevelRequest
{
public string Level { get; set; }
}

View File

@@ -1,3 +1,4 @@
using Common.Configuration.General;
using Common.Configuration.Logging;
using Domain.Enums;
using Infrastructure.Configuration;
@@ -5,6 +6,7 @@ using Infrastructure.Logging;
using Infrastructure.Verticals.ContentBlocker;
using Infrastructure.Verticals.DownloadCleaner;
using Infrastructure.Verticals.QueueCleaner;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AspNetCore.SignalR;
using Serilog;
using Serilog.Core;
@@ -16,13 +18,25 @@ namespace Executable.DependencyInjection;
public static class LoggingDI
{
public static ILoggingBuilder AddLogging(this ILoggingBuilder builder, IConfiguration configuration, IServiceProvider serviceProvider)
public static ILoggingBuilder AddLogging(this ILoggingBuilder builder, IServiceProvider serviceProvider)
{
// Get the logging configuration
LoggingConfig? config = configuration.GetSection(LoggingConfig.SectionName).Get<LoggingConfig>();
// Register LoggingConfigManager as a singleton
serviceProvider.GetRequiredService<IServiceCollection>()
.TryAddSingleton<LoggingConfigManager>();
// Get LoggingConfigManager (will be created if not already registered)
var configManager = serviceProvider.GetRequiredService<LoggingConfigManager>();
// Get the dynamic level switch for controlling log levels
var levelSwitch = configManager.GetLevelSwitch();
// Get the configuration path provider
var pathProvider = serviceProvider.GetRequiredService<ConfigurationPathProvider>();
// Get logging config from the config manager
var config = serviceProvider.GetRequiredService<ConfigManager>()
.GetConfiguration<GeneralConfig>().Logging;
// Create the logs directory
string logsPath = Path.Combine(pathProvider.GetConfigPath(), "logs");
@@ -58,8 +72,7 @@ public static class LoggingDI
List<string> instanceNames = [InstanceType.Sonarr.ToString(), InstanceType.Radarr.ToString(), InstanceType.Lidarr.ToString()];
int arrPadding = instanceNames.Max(x => x.Length) + 2;
// Set the minimum log level
LogEventLevel level = config?.LogLevel ?? LogEventLevel.Information;
// Log level is controlled by the LoggingConfigManager's level switch
// Apply padding values to templates
string consoleTemplate = consoleOutputTemplate
@@ -72,9 +85,9 @@ public static class LoggingDI
.Replace("JOB_PAD", jobPadding.ToString())
.Replace("ARR_PAD", arrPadding.ToString());
// Configure base logger
// Configure base logger with dynamic level control
logConfig
.MinimumLevel.Is(level)
.MinimumLevel.ControlledBy(levelSwitch)
.Enrich.FromLogContext()
.WriteTo.Console(new ExpressionTemplate(consoleTemplate, theme: TemplateTheme.Literate));

View File

@@ -8,7 +8,12 @@ builder.Services
.AddInfrastructure(builder.Configuration)
.AddApiServices();
builder.Logging.AddLogging(builder.Configuration, builder.Services.BuildServiceProvider());
// Register services needed for logging first
builder.Services.AddSingleton<Infrastructure.Logging.LoggingConfigManager>();
// Add logging with proper service provider
var serviceProvider = builder.Services.BuildServiceProvider();
await builder.Logging.AddLogging(serviceProvider);
var app = builder.Build();