TFS_ANTIGUO/TPAtesa_Davivienda/FE/DAVIVIENDA.SUGESE/DAVIVIENDA.SUGESE.ControlExcepciones/LoggerManager.cs
2026-06-26 10:05:33 -06:00

186 lines
5.4 KiB
C#

using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace DAVIVIENDA.SUGESE.ControlExcepciones
{
public class LoggerManager: ILoggerManager
{
private readonly ILog _logger = LogManager.GetLogger(typeof(LoggerManager));
public const string _systemName = "SUGESE";
public LoggerManager()
{
try
{
XmlDocument log4netConfig = new XmlDocument();
using (var fs = File.OpenRead("log4net.config"))
{
log4netConfig.Load(fs);
var repo = LogManager.CreateRepository(
Assembly.GetEntryAssembly(),
typeof(log4net.Repository.Hierarchy.Hierarchy));
string ruta = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
GlobalContext.Properties["FilePath"] = ruta.Substring(6, ruta.Length - 6);
XmlConfigurator.Configure(repo, log4netConfig["log4net"]);
}
}
catch (Exception ex)
{
_logger.Error("Error", ex);
}
}
public void LogInformation(string message)
{
_logger.Info(message);
}
public void LogInformation(string message, Exception ex)
{
_logger.Info(message, ex);
}
public void LogWarning(string message)
{
_logger.Warn(message);
}
public void LogWarning(string message, Exception ex)
{
_logger.Warn(message, ex);
}
public void LogError(string message)
{
_logger.Error(message);
}
public void LogError(string message, Exception ex)
{
_logger.Error(message, ex);
}
public void ManageExceptions(string exc, object trama)
{
ManageExceptions(new Exception(exc), trama, null);
}
public string ManageExceptions(Exception exc, object trama, string idPantalla)
{
try
{
if (exc is System.Threading.ThreadAbortException)
{
return string.Empty;
}
Dictionary<string, object> DatosAgregados = new Dictionary<string, object>();
if (idPantalla != null)
DatosAgregados.Add("Clase:", idPantalla);
if (trama != null)
DatosAgregados.Add("Trama:", SerializarObjeto(trama));
if (idPantalla == null)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
int idx = 0;
string strClase = st.GetFrame(idx).GetMethod().ReflectedType.FullName;
while (idx < st.FrameCount)
{
if (strClase.Contains("LoggerManager"))
{
idx++;
strClase = st.GetFrame(idx).GetMethod().ReflectedType.FullName;
}
else
{
break;
}
}
string strNombreMetodo = st.GetFrame(idx).GetMethod().Name;
DatosAgregados.Add("Clase:", (strClase == null ? "" : strClase));
DatosAgregados.Add("Metodo:", (strNombreMetodo == null ? "" : strNombreMetodo));
}
//if (HttpContext.Current != null)
//{
// DatosAgregados.Add("Usuario:", HttpContext.Current.User.Identity.Name);
// DatosAgregados.Add("IP:", HttpContext.Current.Request.UserHostName);
// DatosAgregados.Add("URL:", HttpContext.Current.Request.Url.ToString());
//}
string codError = (idPantalla != null ? idPantalla : _systemName) + "-" + DateTime.Now.ToString("yyyyMMdd-HHmmss");
DatosAgregados.Add("Codigo Error:", codError);
_logger.Error(DatosAgregados, exc);
return codError;
}
catch
{
return string.Empty;
}
}
public string ManageExceptions(Exception exc)
{
return ManageExceptions(exc, null, null);
}
public void ManageExceptions(Exception exc, object trama)
{
ManageExceptions(exc, trama, null);
}
public static string SerializarObjeto(object trama)
{
string ret = string.Empty;
try
{
if (trama != null)
{
XmlSerializer xs = new XmlSerializer(trama.GetType());
StringWriter stringWriter = new StringWriter();
xs.Serialize(stringWriter, trama);
ret = stringWriter.ToString();
if (ret.Length > (15 * 1024)) // 15 MB, no serializar objetos mas grandes que.
ret = string.Empty;
}
}
catch
{
}
return ret;
}
}
}