c# 程序检测日志输出的类

public class LogWrite
    {
        public LogWrite()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        #region Constant
        //单个日志文件大小
        private const long FILESIZE =  *  * ;
 
        //日志文件数
        private const int LOGFILENUM = ;
 
        //日志类型
        private const short DEBUG = ;
        private const short INFO = ;
        private const short WARNING = ;
        private const short ERROR = ;
 
        //日志级别
        private const short LOGLEVEL = DEBUG;
        #endregion
 
        #region Members
 
        //日志文件路径
        private string m_LogDir = System.Windows.Forms.Application.StartupPath + "\\module0\\Log";
        private string m_filePath = System.Windows.Forms.Application.StartupPath + "\\module0\\Log\\Log.txt";//工作日志文件
 
        private static LogWrite m_Log = null;
        private Object objLock = new Object();
        private string destClass = string.Empty;
        #endregion
 
        #region Properties
        /// <summary>
        /// 记录日志的类名
        /// </summary>
        public string DestClass
        {
            get { return destClass; }
            set { destClass = value; }
        }
 
        #endregion
 
        #region Ctor
 
        #endregion
 
        #region Public Methods
 
        /// <summary>
        /// 创建日志类实例
        /// </summary>
        /// <returns></returns>
        public static LogWrite CreateInstance()
        {
            if (m_Log == null)
            {
                m_Log = new LogWrite();
            }
            return m_Log;
        }
 
 
        /// <summary>
        /// 写信息日志
        /// </summary>
        /// <param name="content"></param>
        public void Info(string content)
        {
            if (LOGLEVEL <= )
            {
                lock (objLock)
                {
                    WriteLog(content, LogWrite.INFO);
                }
            }
        }
 
        /// <summary>
        /// 写调试日志
        /// </summary>
        /// <param name="content"></param>
        public void Debug(string content)
        {
            if (LOGLEVEL <= )
            {
                lock (objLock)
                {
                    WriteLog(content, LogWrite.DEBUG);
                }
            }
        }
 
        /// <summary>
        /// 写警告日志
        /// </summary>
        /// <param name="content"></param>
        public void Warning(string content)
        {
            if (LOGLEVEL <= )
            {
                lock (objLock)
                {
                    WriteLog(content, LogWrite.WARNING);
                }
            }
        }
 
        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="content"></param>
        public void Error(string content)
        {
            if (LOGLEVEL <= )
            {
                lock (objLock)
                {
                    WriteLog(content, LogWrite.ERROR);
                }
            }
        }
 
 
        #endregion
 
        #region Private Methods
 
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="content"></param>
        /// <param name="type"></param>
        private void WriteLog(string content, short type)
        {
            if (content == null || content.Equals(string.Empty))
            {
                return;
            }
            try
            {
                if (!File.Exists(m_filePath))
                {
                    CreateWorkLogFile();
                }
 
                if (LogFileIsTooLarge(m_filePath))
                {
                    ProcessLogFile();
                }
 
                string strType = "";
                switch (type)
                {
                    case DEBUG:
                        strType = " [DEBUG] ";
                        break;
                    case INFO:
                        strType = " [INFO] ";
                        break;
                    case WARNING:
                        strType = " [WARNING] ";
                        break;
                    case ERROR:
                        strType = " [ERROR] ";
                        break;
                    default:
                        strType = " [INFO] ";
                        break;
                }
                string strContent = string.Format("{0}\t{1}\t[{2}]\t{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), strType, DestClass, content);
                StreamWriter sWriter = new StreamWriter(m_filePath, true, System.Text.Encoding.Default);
                sWriter.WriteLine(strContent);
                sWriter.Flush();
                sWriter.Close();
                sWriter = null;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 创建工作日志文件
        /// </summary>
        private void CreateWorkLogFile()
        {
            if (!Directory.Exists(m_LogDir))
            {
                Directory.CreateDirectory(m_LogDir);
            }
            FileStream fStream = new FileStream(m_filePath, FileMode.CreateNew);
            fStream.Flush();
            fStream.Close();
            fStream = null;
        }
 
 
        /// <summary>
        /// 判断日志文件是否超过预定义最大值
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private bool LogFileIsTooLarge(string fileName)
        {
            FileInfo fi = new FileInfo(fileName);
            if (fi.Length >= FILESIZE)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
 
        /// <summary>
        /// 处理日志文件(工作日志文件超过最大值)
        /// </summary>
        private void ProcessLogFile()
        {
            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(m_LogDir);
                FileInfo[] vFileInfo = dirInfo.GetFiles("*.txt");
                if (vFileInfo != null && vFileInfo.Length > LOGFILENUM)
                {
                    FileInfo oldFile = vFileInfo[];
                    foreach (FileInfo fi in vFileInfo)
                    {
                        if (DateTime.Compare(fi.LastWriteTime, oldFile.LastWriteTime) < )
                        {
                            oldFile = fi;
                        }
                    }
                    File.Delete(oldFile.FullName);
                }
                File.Copy(m_filePath, m_LogDir + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt");
                File.Delete(m_filePath);
                CreateWorkLogFile();
            }
            catch
            {
 
            }
        }
 
        #endregion
    }

类的路径需要修改下,就OK 了

private string m_LogDir = System.Windows.Forms.Application.StartupPath + "\\module0\\Log";
private string m_filePath = System.Windows.Forms.Application.StartupPath + "\\module0\\Log\\Log.txt";//工作日志文件

用的时候 只要实例就行了

private LogWrite m_Log = null;

//构造函数里
m_Log = module0.WritLog.LogWrite.CreateInstance();
m_Log.set_DestClass
//输出日志在地方
m_Log.Info(System.DateTime.get_Now().ToString() + " :... ");
上一篇:mysql根据时间统计数据语句


下一篇:转载:C#中委托、事件与Observer设计模式