示例#1
0
        /// <summary>
        /// Formateo del texto del mensale
        /// </summary>
        /// <param name="_mensaje">Mensaje a publicar</param>
        /// <param name="_nivel">Nivel de advertencias</param>
        /// <returns></returns>
        internal string formatoMensaje(string _mensaje, Basicos.Niveles _nivel)
        {
            // Variables gestion respuesta
            string strRespuesta = string.Empty;

            // Variables internas
            string mensaje = _mensaje;

            Basicos.Niveles nivel        = _nivel;
            string          strNivel     = string.Empty;
            string          strFechaHora = string.Empty;

            // Crear el mensaje
            if (nivel == Basicos.Niveles.Depuracion)
            {
                strNivel = "[D]";
            }
            else if (nivel == Basicos.Niveles.Informacion)
            {
                strNivel = "[+]";
            }
            else if (nivel == Basicos.Niveles.Advertencia)
            {
                strNivel = "[!]";
            }
            else if (nivel == Basicos.Niveles.Error)
            {
                strNivel = "[-]";
            }
            else if (nivel == Basicos.Niveles.Fatal)
            {
                strNivel = "[*]";
            }
            // No comple...
            else
            {
                strNivel = "[?]";
            }

            // Formatear fecha y hora
            DateTime dtFechaHora = DateTime.Now;

            strFechaHora = string.Format("{0:dd/MM/yyyy - HH:mm:ss}", dtFechaHora);

            // Generar mensaje
            strRespuesta = string.Format("{0} {1} --> {2}", strNivel, strFechaHora, mensaje);

            // Respuesta
            return(strRespuesta);
        }
示例#2
0
        /// <summary>
        /// Función para la pruebas unitarias
        /// Quitar al final
        /// </summary>
        /// <param name="_mensaje"></param>
        /// <param name="_nivel"></param>
        /// <param name="_strError"></param>
        /// <returns></returns>
        // public bool Ficheros(string _mensaje, Basicos.Niveles _nivel, out KeyValuePair<string, string> _strError)
        // {
        //     // Variables gestion respuesta
        //     bool bRespuesta = false;
        //
        //     bRespuesta = grabarFichero(_mensaje, _nivel, out _strError);
        //
        //     // Respuesta
        //     return bRespuesta;
        // }
        /// <summary>
        /// Función que escribe en un fichero de log, los mensajes
        /// </summary>
        /// <param name="_mensaje">Mensaje que deseamos publicar</param>
        /// <param name="_nivel">Nivel de imporancia del mensaje</param>
        /// <param name="_strError">Si se produce un error, devuelve la descripción de el</param>
        /// <returns>True --> ejecución correcta, False --> Se ha producido un error en la ejecución</returns>
        internal bool publicarFichero(string _mensaje, Basicos.Niveles _nivel, out KeyValuePair <string, string> _strError)
        {
            // Variables para gestionar la respuesta
            bool bRespuesta = false;
            KeyValuePair <string, string> strError = new KeyValuePair <string, string>();

            // Variables internas
            string mensaje = _mensaje;

            Basicos.Niveles nivel           = _nivel;
            string          mensajeGrabar   = string.Empty;
            string          completoFichero = rutaFichero + nombreFichero;
            StreamWriter    fichero;

            // Saber si el fichero existe
            if (File.Exists(completoFichero))
            {
                // Agregamos las lineas al fichero existente
                fichero = File.AppendText(completoFichero);
            }
            else
            {
                // El fichero no existe, lo creamos
                fichero = File.CreateText(completoFichero);
            }

            // formateo del mensaje
            mensajeGrabar = formatoMensaje(mensaje, nivel);

            // Grabar el mensaje en el fichero
            try
            {
                fichero.WriteLine(mensajeGrabar);
                bRespuesta = true;
            }
            catch (Exception ex)
            {
                strError   = new KeyValuePair <string, string>("ERROR_NO_DEFINIDO", ex.Message);
                bRespuesta = false;
            }

            // Cerrar fichero
            fichero.Close();

            // Respuesta
            _strError = strError;
            return(bRespuesta);
        }
示例#3
0
        public bool publicarLog(string _mensaje, Basicos.Niveles _nivel, out KeyValuePair <string, string> _strError)
        {
            // Variables para procesar la respuesta
            bool bRespuesta = false;
            KeyValuePair <string, string> strError = new KeyValuePair <string, string>();

            // Variables internas
            string mensaje = _mensaje;

            Basicos.Niveles nivel = _nivel;

            if ((modo != Basicos.Modos.Completo) && (modo != Basicos.Modos.Fichero) && (modo != Basicos.Modos.Sistema))
            {
                strError   = new KeyValuePair <string, string>("ERROR_MODO_NO_VALIDO", "Error en el modo de publicación.");
                bRespuesta = false;
            }
            else
            {
                if ((modo == Basicos.Modos.Completo) || (modo == Basicos.Modos.Fichero))
                {
                    // Publicamos en el fichero
                    bRespuesta = publicarFichero(mensaje, nivel, out strError);
                }
                else
                {
                    // Nada
                }

                if ((modo == Basicos.Modos.Completo) || (modo == Basicos.Modos.Sistema))
                {
                    // Publicamos en el visor de sucesos
                    bRespuesta = publicarSucesos(mensaje, nivel, out strError);
                }
                else
                {
                    // Nada
                }
            }


            // Respuesta
            _strError = strError;
            return(bRespuesta);
        }
示例#4
0
        /// <summary>
        /// Para verificar que la publicación en el visor de sucesos funciona
        /// Quitar al final
        /// </summary>
        /// <param name="_mensaje"></param>
        /// <param name="_nivel"></param>
        /// <param name="_strError"></param>
        /// <returns></returns>
        // public bool Sucessos(string _mensaje, Basicos.Niveles _nivel, out KeyValuePair<string, string> _strError)
        // {
        //     bool bRespuesta = false;
        //
        //     bRespuesta = grabarSucesos(_mensaje, _nivel, out _strError);
        //
        //     // Respuesta
        //     return bRespuesta;
        // }
        /// <summary>
        /// Función para publicar en el gestor de eventos
        /// </summary>
        /// <param name="_mensaje">Mensaje que vamos a publicar</param>
        /// <param name="_nivel">Nivel de importancia del mensaje</param>
        /// <param name="_strError">Si se produce un error, devuelve la descripción de el</param>
        /// <returns>True --> ejecución correcta, False --> Se ha producido un error en la ejecución</returns>
        /// <remarks>Para que funcione bien tiene que estar creada la clave en el registro.
        /// Para que funcione la publicación en el visor de eventos, hay que crear la siguiente clave en el registro:
        /// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\Prueba
        /// Donde "Prueba" es el nombre de origen en el visor de eventos</remarks>
        internal bool publicarSucesos(string _mensaje, Basicos.Niveles _nivel, out KeyValuePair <string, string> _strError)
        {
            // Variables gestion respuesta
            bool bRespuesta = false;
            KeyValuePair <string, string> strError = new KeyValuePair <string, string>();

            // Variables internas
            string sSource;
            string mensaje       = _mensaje;
            string mensajeGrabar = string.Empty;

            Basicos.Niveles   nivel       = _nivel;
            EventLogEntryType nivelEvento = new EventLogEntryType();

            // Establece el origen en el visor de eventos
            if (string.IsNullOrEmpty(aplicacion))
            {
                sSource = APLICACION;
            }
            else
            {
                sSource = aplicacion;
            }


            // Extraer nivel
            switch (nivel)
            {
            case Basicos.Niveles.Advertencia:
                nivelEvento = EventLogEntryType.Warning;
                break;

            case Basicos.Niveles.Completo:
                nivelEvento = EventLogEntryType.Information;
                break;

            case Basicos.Niveles.Depuracion:
                nivelEvento = EventLogEntryType.Information;
                break;

            case Basicos.Niveles.Error:
                nivelEvento = EventLogEntryType.Error;
                break;

            case Basicos.Niveles.Fatal:
                nivelEvento = EventLogEntryType.Information;
                break;

            case Basicos.Niveles.Informacion:
                nivelEvento = EventLogEntryType.Information;
                break;

            case Basicos.Niveles.Ninguno:
                nivelEvento = EventLogEntryType.Information;
                break;

            default:
                nivelEvento = EventLogEntryType.Error;
                break;
            }

            // Formatear mensaje
            mensajeGrabar = formatoMensaje(mensaje, nivel);

            try
            {
                EventLog.WriteEntry(sSource, mensajeGrabar, nivelEvento, idEvento, categoria);
                bRespuesta = true;
            }
            catch (Exception ex)
            {
                strError   = new KeyValuePair <string, string>("ERROR_EVENTO", ex.Message);
                bRespuesta = false;
            }

            // Respuesta
            _strError = strError;
            return(bRespuesta);
        }