Files
Asegasa.NET/bdAsegasa/Utilidades.cs

144 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using bdAsegasa.dbcontext;
using Microsoft.VisualBasic;
namespace bdAsegasa.db
{
public class Utilidades
{
public static string Usuario;
public static int idUsuario;
public static usuarios UsuarioActual;
public static string VersionPrograma { get; set; }
public static EventLog el;
public static string DirectorioLogs;
private static object oBloqueoLog = new object();
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:
// ===============
lock (oBloqueoLog)
{
string sFicheroLog = DirectorioLogs + "Log-" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + ".txt";
try
{
// if (!(e == null))
if (e != null) //davrod
{
if (el is not null)
el.WriteEntry(e.Message + Constants.vbCrLf + e.StackTrace, EventLogEntryType.Error);
string sStackTrace = "Tipo excepción: " + e.ToString() + Constants.vbCrLf;
var exError = e;
do
{
sStackTrace += exError.StackTrace + Constants.vbCrLf;
exError = exError.InnerException;
}
while (exError != null);
if (!string.IsNullOrEmpty(sStackTrace))
Mensaje += Constants.vbCrLf + "StackTrace: " + sStackTrace;
Anadelogtxt(Mensaje + " --- " + e.StackTrace, sFicheroLog);
}
switch (Tipo)
{
case tsUtilidades.Enumeraciones.TipoLog.Fallo:
case tsUtilidades.Enumeraciones.TipoLog.Advertencia:
{
if (Tipo == tsUtilidades.Enumeraciones.TipoLog.Fallo)
{
sFicheroLog = DirectorioLogs + "Errores-" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + ".txt";
Asunto = "Error Servicio Tecnosis. " + ". Version:" + VersionPrograma + ". " + Asunto;
Mensaje = "Error Servicio Tecnosis. " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " Enviado desde " + Environment.MachineName + ". Version:" + VersionPrograma + ". Mensaje: " + Mensaje;
}
else
{
Asunto = "Advertencia Servicio Tecnosis. " + ". Version:" + VersionPrograma + ". " + Asunto;
Mensaje = "Advertencia Servicio Tecnosis. " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " Enviado desde " + Environment.MachineName + ". Version:" + VersionPrograma + ". " + Mensaje;
}
string sDireccionesEnvio = "sevilla@tecnosis.net";
string sServidorSMTP = "mail.tecnosis.net";
string sRemitente = "logs@tecnosis.es";
tsUtilidades.Correo.Funciones.EnviaCorreo(sServidorSMTP, sRemitente, sDireccionesEnvio, Asunto, Mensaje, null, "", "", sRemitente, "jtvdzklmujkechje", 587, true); //Cambiar contraseña de logs -> LoGs20i9.
break;
}
}
// Anadelogtxt(Mensaje + " --- " + e.StackTrace, sFicheroLog);
}
catch (Exception ex)
{
sFicheroLog = DirectorioLogs + @"Errores\Errores-" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + ".txt";
if (e is not 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.Now.ToString() + "|" + "Ws: " + Process.GetCurrentProcess().WorkingSet64.ToString().PadLeft(20) + " PMS: " + Process.GetCurrentProcess().PrivateMemorySize64.ToString().PadLeft(20) + "|" + Mensaje;
sw.WriteLine(Mensaje);
}
catch (Exception ex)
{
try
{
string sDireccionesEnvio = "sevilla@tecnosis.net";
string sServidorSMTP = "mail.tecnosis.net";
string sRemitente = "logs@tecnosis.es";
// tsUtilidades.Correo.Funciones.EnviaCorreo(sServidorSMTP, sRemitente, sDireccionesEnvio, Asunto, Mensaje,,,,, "se9608dc.g")
tsUtilidades.Correo.Funciones.EnviaCorreo(sServidorSMTP, sRemitente, sDireccionesEnvio, "Error Anadelogtxt. " + Mensaje, Environment.MachineName + ".- " + ex.Message + Constants.vbCrLf + ex.StackTrace + Constants.vbCrLf + ex.Source, null, "", "", sRemitente, "jtvdzklmujkechje", 587, true); //Cambiar contraseña de logs -> LoGs20i9.
}
catch (Exception ex2)
{
}
}
finally
{
try
{
sw.Close();
}
catch
{
}
}
}
}
}