Feat Workers Prioridades y Nivel Serilog

This commit is contained in:
2025-09-06 21:44:52 -03:00
parent f384a640f3
commit fa92d9638c
29 changed files with 2068 additions and 95 deletions

View File

@@ -6,6 +6,7 @@ using Elecciones.Core.DTOs.ApiRequests;
using Elecciones.Database.Entities;
using Microsoft.AspNetCore.Authorization;
using Elecciones.Core.Enums;
using Elecciones.Infrastructure.Services;
namespace Elecciones.Api.Controllers;
@@ -16,11 +17,13 @@ public class AdminController : ControllerBase
{
private readonly EleccionesDbContext _dbContext;
private readonly ILogger<AdminController> _logger;
private readonly LoggingSwitchService _loggingSwitchService;
public AdminController(EleccionesDbContext dbContext, ILogger<AdminController> logger)
public AdminController(EleccionesDbContext dbContext, ILogger<AdminController> logger, LoggingSwitchService loggingSwitchService)
{
_dbContext = dbContext;
_logger = logger;
_loggingSwitchService = loggingSwitchService;
}
// Endpoint para obtener todas las agrupaciones para el panel de admin
@@ -297,4 +300,41 @@ public class AdminController : ControllerBase
await _dbContext.SaveChangesAsync();
return NoContent();
}
/// <summary>
/// Actualiza el nivel mínimo de logging en tiempo real y guarda la configuración en la BD.
/// </summary>
/// <param name="request">Un objeto que contiene el nuevo nivel de logging.</param>
[HttpPut("logging-level")]
public async Task<IActionResult> UpdateLoggingLevel([FromBody] UpdateLoggingLevelRequest request)
{
if (string.IsNullOrWhiteSpace(request.Level))
{
return BadRequest("El nivel de logging no puede estar vacío.");
}
// 1. Intentamos actualizar el interruptor de Serilog en memoria.
bool success = _loggingSwitchService.SetLoggingLevel(request.Level);
if (!success)
{
return BadRequest($"El nivel de logging '{request.Level}' no es válido. Los valores posibles son: Verbose, Debug, Information, Warning, Error, Fatal.");
}
// 2. Si el cambio fue exitoso, guardamos el nuevo valor en la base de datos.
var config = await _dbContext.Configuraciones.FindAsync("Logging_Level");
if (config == null)
{
_dbContext.Configuraciones.Add(new Configuracion { Clave = "Logging_Level", Valor = request.Level });
}
else
{
config.Valor = request.Level;
}
await _dbContext.SaveChangesAsync();
_logger.LogWarning("El nivel de logging ha sido cambiado a: {Level}", request.Level);
return Ok(new { message = $"Nivel de logging actualizado a '{request.Level}'." });
}
}