60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
using Dapper;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Serilog;
|
|
using Shared.DTO.VariablesEntorno;
|
|
|
|
namespace Shared.Helper
|
|
{
|
|
public class FileLoggerHelper
|
|
{
|
|
public static void ConfigureLogger(IConfiguration configuration)
|
|
{
|
|
var logFilePath = configuration.GetSection("Logging:LogFile:Path").Value;
|
|
var logFileFullPath = Path.Combine(Directory.GetCurrentDirectory(), logFilePath);
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
.ReadFrom.Configuration(configuration)
|
|
.WriteTo.File(logFileFullPath, rollingInterval: RollingInterval.Day)
|
|
.CreateLogger();
|
|
}
|
|
|
|
public static void LogInformation(string message)
|
|
{
|
|
Log.Information($"{message}");
|
|
}
|
|
|
|
public static void LogError(string message, Exception ex)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|