Files
2026-01-23 12:45:41 +01:00

117 lines
5.7 KiB
C#

using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ServicioGrupoSanchoToro
{
internal class Logs
{
private static object oBloqueoLog;
private static EventLog el;
public static void AñadeLog(tsUtilidades.Enumeraciones.TipoLog Tipo, string Asunto, string Mensaje, Exception e = null)
{
// ----------------------------------------------------------------------------------------------------
// Descripción Sub: Gestión de logs de la aplicación
// Fecha. Creacion: ???
// Creada por: manmog
// Ultima Modificacion: 24/11/2010
//
// Modificaciones:
// ===============
if (oBloqueoLog == null) oBloqueoLog = new object();
lock (oBloqueoLog)
{
string sFicheroLog = Procesos.Conf.DirectorioLogs + "Log-" + DateTime.UtcNow.Year.ToString() + DateTime.UtcNow.Month.ToString() + ".txt";
try
{
if (e != null)
{
if (el != null)
el.WriteEntry(e.Message + Constants.vbCrLf + e.StackTrace, EventLogEntryType.Error);
string sStackTrace = "Tipo excepción: " + e.ToString() + Constants.vbCrLf;
Exception exError = e;
do
{
sStackTrace += exError.StackTrace + Constants.vbCrLf;
exError = exError.InnerException;
}
while (!Information.IsNothing(exError));
if (sStackTrace != "")
Mensaje += Constants.vbCrLf + "StackTrace: " + sStackTrace;
}
if (Tipo == tsUtilidades.Enumeraciones.TipoLog.Fallo)
{
sFicheroLog = Procesos.Conf.DirectorioLogs + "Errores-" + DateTime.UtcNow.Year.ToString() + DateTime.UtcNow.Month.ToString().PadLeft(2, '0') + ".txt";
Asunto = "Error GrupoSanchoToro. " + ". Version:" + bdGrupoSanchoToro.db.Utilidades.VersionPrograma + ". " + Asunto;
Mensaje = "Error GrupoSanchoToro. " + DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss") + " Enviado desde " + Environment.MachineName + ". Version:" + bdGrupoSanchoToro.db.Utilidades.VersionPrograma + ". Mensaje: " + Mensaje;
}
else
{
Asunto = "Advertencia GrupoSanchoToro. " + ". Version:" + bdGrupoSanchoToro.db.Utilidades.VersionPrograma + ". " + Asunto;
Mensaje = "Advertencia GrupoSanchoToro. " + DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss") + " Enviado desde " + Environment.MachineName + ". Version:" + bdGrupoSanchoToro.db.Utilidades.VersionPrograma + ". " + Mensaje;
}
string sDireccionesEnvio = "manmog@gmail.com";
string sServidorSMTP = "smtp.gmail.com";
var sRemitente = "administracion@english-skills.net";
tsCorreos.Funciones.EnviaCorreo(sServidorSMTP, sRemitente, sDireccionesEnvio, Asunto, Mensaje,null,null,"","", sRemitente, "", 587, true); // ant: ksswsleqfvmzzbig
Anadelogtxt(Mensaje + " --- " + ((e==null) ? "" : e.StackTrace), sFicheroLog);
}
catch (Exception ex)
{
sFicheroLog = Procesos.Conf.DirectorioLogs + @"Errores\Errores-" + DateTime.UtcNow.Year.ToString() + DateTime.UtcNow.Month.ToString().PadLeft(2, '0') + ".txt";
if (e != null)
Mensaje += " --- " + e.StackTrace;
Anadelogtxt(Mensaje + " ---" + ex.Message + " --- " + ex.StackTrace, sFicheroLog);
}
}
}
public static void Anadelogtxt(string Mensaje, string FicheroLog)
{
System.IO.StreamWriter sw = null;
try
{
Mensaje = Mensaje.Replace(Constants.vbCrLf, "---");
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(FicheroLog)))
tsUtilidades.Utilidades.CreaEstructuraDirectorio(System.IO.Path.GetDirectoryName(FicheroLog));
if (System.IO.File.Exists(FicheroLog))
sw = System.IO.File.AppendText(FicheroLog);
else
sw = System.IO.File.CreateText(FicheroLog);
Mensaje = DateTime.UtcNow.ToString() + "|" + Mensaje;
sw.WriteLine(Mensaje);
}
catch (Exception ex)
{
try
{
string sDireccionesEnvio = "manmog@gmail.com";
string sServidorSMTP = "smtp.gmail.com";
var sRemitente = "administracion@english-skills.net";
tsCorreos.Funciones.EnviaCorreo(sServidorSMTP, sRemitente, sDireccionesEnvio, "Error Anadelogtxt. " + Mensaje, Environment.MachineName + ".- " + ex.Message + Constants.vbCrLf + ex.StackTrace + Constants.vbCrLf + ex.Source, null,null,"","", sRemitente, "", 587, true);
}
catch (Exception ex2)
{
}
}
finally
{
try
{
sw.Close();
}
catch
{
}
}
}
}
}