feat: Se implementan logs para auditoria

This commit is contained in:
Leonel Toro 2025-07-02 11:37:41 -04:00
parent 4b6204d9e7
commit d455289087
3 changed files with 150 additions and 90 deletions

View file

@ -21,10 +21,11 @@ namespace BLL.Recuperacion_DGA
public async Task<bool> RegistrarMedicionesAsync()
{
WriteLineAndLog("Iniciando el proceso de recuperación DGA...");
try
{
await WriteLineAndLog("INICIO", "Inicio proceso de recuperación DGA", "");
WriteLineAndLog("Obteniendo Mediciones Scada", ConsoleColor.Green);
var mediciones = await _dGAMedicionScadaRepository.ObtenerMedicionesAsync();
foreach (var medicion in mediciones)
{
try
@ -64,7 +65,7 @@ namespace BLL.Recuperacion_DGA
Totalizador = medicion.Totalizador.ToString() ?? "",
}
};
await _registrarMedicion.EnviarMedicionAsync(medicion.Code, body, medicion.Id);
//await _registrarMedicion.EnviarMedicionAsync(medicion.Code, body, medicion.Id);
}
}
@ -74,7 +75,10 @@ namespace BLL.Recuperacion_DGA
WriteLineAndLog($"Error al enviar la medición con ID {medicion.Code}.", ConsoleColor.Red);
}
}
await WriteLineAndLog("FIN","Fin proceso de recuperación DGA","");
await WriteLineAndLog("INICIO", "Inicio proceso de recuperación DGA los Vilos", "VILOS");
WriteLineAndLog("Obteniendo Mediciones Scada los Vilos", ConsoleColor.Green);
var medicionesVilos = await _dgaMedicionScadaVilosRepository.ObtenerMedicionesVilosAsync();
foreach (var medicionVilos in medicionesVilos)
{
@ -111,7 +115,7 @@ namespace BLL.Recuperacion_DGA
}
};
await _registrarMedicion.EnviarMedicionAsync(medicionVilos.Code, body, medicionVilos.Id);
//await _registrarMedicion.EnviarMedicionAsync(medicionVilos.Code, body, medicionVilos.Id);
}
}
@ -121,7 +125,14 @@ namespace BLL.Recuperacion_DGA
WriteLineAndLog($"Error al enviar la medición vilos con ID {medicionVilos.Code}.", ConsoleColor.Red);
}
}
await WriteLineAndLog("FIN", "Fin proceso de recuperación DGA los Vilos", "VILOS");
}
catch (Exception ex)
{
FileLoggerHelper.LogError($"[Error] {ex.Message}.", ex);
WriteLineAndLog($"Error al procesar las mediciones.", ConsoleColor.Red);
return false;
}
return true;
}
@ -140,5 +151,21 @@ namespace BLL.Recuperacion_DGA
FileLoggerHelper.LogInformation($"{msj}");
}
static async Task WriteLineAndLog(string evento, string proceso, string operacion = null, ConsoleColor? color = null)
{
if (color.HasValue && Enum.IsDefined(typeof(ConsoleColor), color.Value))
{
Console.ForegroundColor = (ConsoleColor)color;
Console.WriteLine($"{proceso}");
Console.ResetColor();
}
else
{
Console.WriteLine($"{proceso}");
}
FileLoggerHelper.LogInformation($"{proceso}");
await FileLoggerHelper.InsertarLogsAsync(evento,proceso,operacion);
}
}
}

View file

@ -1,5 +1,8 @@
using Microsoft.Extensions.Configuration;
using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Serilog;
using Shared.DTO.VariablesEntorno;
namespace Shared.Helper
{
@ -25,5 +28,33 @@ namespace Shared.Helper
{
Log.Error(ex, message);
}
public static async Task<bool> InsertarLogsAsync(string evento, string proceso, string operacion)
{
try
{
using (var connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
string sql = @"INSERT INTO DGA_LOGS_REGISTROS_ENVIOS (evento, proceso, operacion)
VALUES (@evento, @proceso, @operacion)";
await connection.ExecuteAsync(sql, new
{
evento,
proceso,
operacion
});
return true;
}
}
catch (Exception e)
{
LogError("Error al insertar logs", e);
return false;
}
}
}
}

View file

@ -7,6 +7,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.66" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.6" />
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />