⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.cs

📁 一个用于两地间交通方案选择的小决策支持系统
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

/*
 * Copyright by DigitalSupport
 * summary:  This is used for Network Diagnosis Project
 * version:  1.0.0
 * author:   Zhang Haoting @ 2006.12.11
 * email:    zht_china@126.com
 * 
 */
namespace DigitalSupport
{
    /// <summary>
    /// This class is desigen for logging error when meet a exception 
    /// </summary>
    public class Log
    {
        #region Private Members
        /// <summary>
        /// Log File Path
        /// </summary>
        private string logFile = null;
        /// <summary>
        /// Operation time
        /// </summary>
        private string strTime = null;
        /// <summary>
        /// Module description
        /// </summary>
        private string strModule = null;
        /// <summary>
        /// Error description
        /// </summary>
        private string strDescription = null;
        #endregion

        #region Public Properties
        /// <summary>
        /// Error description
        /// </summary>
        public string Description
        {
            get { return this.strDescription; }
            set { this.strDescription = value; }
        }
        /// <summary>
        /// Module location
        /// </summary>
        public string Module
        {
            get { return this.strModule; }
            set { this.strModule = value; }
        }
        #endregion

        /// <summary>
        ///  Default Constructor
        /// </summary>
        public Log()
        {
            this.logFile = ".//log.xml";
            this.strTime = DateTime.Now.ToString();
        }


        /*public Log(string strDescription)
        {
            this.logFile = ".//log.xml";
            this.strTime = DateTime.Now.ToString();
            this.strDescription = strDescription;
        }*/

        /// <summary>
        /// Constructor with exception description and module location
        /// </summary>
        /// <param name="strDescription">Exception description</param>
        /// <param name="strModule">Module location</param>
        public Log(string strDescription, string strModule)
        {
            this.logFile = ".//log.xml";
            this.strTime = DateTime.Now.ToString();
            this.strDescription = strDescription;
            this.strModule = strModule;
        }

        /// <summary>
        /// Write the error information to the log file 
        /// </summary>
        public void WriteLog()
        {
            //this.logFile = "E://a.xml";
            try
            {
                XmlDocument doc = new XmlDocument();
                //first check log file is existing
                FileInfo f = new FileInfo(logFile);
                if (!f.Exists)
                {
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", ""));
                    doc.AppendChild(doc.CreateElement("Logs"));
                    doc.Save(this.logFile);
                }
                doc.Load(this.logFile);
                XmlElement newLog = doc.CreateElement("Log");

                XmlElement newTime = doc.CreateElement("Time");
                newTime.InnerText = this.strTime;
                newLog.AppendChild(newTime);

                XmlElement newModule = doc.CreateElement("Module");
                newModule.InnerText = this.strModule;
                newLog.AppendChild(newModule);

                XmlElement newDescription = doc.CreateElement("Description");
                newDescription.InnerText = this.strDescription;
                newLog.AppendChild(newDescription);

                doc.DocumentElement.AppendChild(newLog);
                doc.Save(this.logFile);
            }
            catch (Exception ex)
            {
                // when cannot logging error
                // it may need some other method(log tise error in the system log file) -
                // to log this error when cannot logging previous error
                // :))
                Console.WriteLine("Unable to Log Error: " + ex.Message); ;
            }
            finally
            {
            }

        }
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -