博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 程序检测日志输出的类
阅读量:5824 次
发布时间:2019-06-18

本文共 5261 字,大约阅读时间需要 17 分钟。

public class LogWrite    {        public LogWrite()        {            //            // TODO: 在此处添加构造函数逻辑            //        }        #region Constant        //单个日志文件大小        private const long FILESIZE = 10 * 1024 * 1024;         //日志文件数        private const int LOGFILENUM = 5;         //日志类型        private const short DEBUG = 0;        private const short INFO = 1;        private const short WARNING = 2;        private const short ERROR = 3;         //日志级别        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        ///         /// 记录日志的类名        ///         public string DestClass        {            get { return destClass; }            set { destClass = value; }        }         #endregion         #region Ctor         #endregion         #region Public Methods         ///         /// 创建日志类实例        ///         /// 
        public static LogWrite CreateInstance()        {            if (m_Log == null)            {                m_Log = new LogWrite();            }            return m_Log;        }          ///         /// 写信息日志        ///         /// 
        public void Info(string content)        {            if (LOGLEVEL <= 1)            {                lock (objLock)                {                    WriteLog(content, LogWrite.INFO);                }            }        }         /// 
        /// 写调试日志        ///         /// 
        public void Debug(string content)        {            if (LOGLEVEL <= 0)            {                lock (objLock)                {                    WriteLog(content, LogWrite.DEBUG);                }            }        }         /// 
        /// 写警告日志        ///         /// 
        public void Warning(string content)        {            if (LOGLEVEL <= 2)            {                lock (objLock)                {                    WriteLog(content, LogWrite.WARNING);                }            }        }         /// 
        /// 写错误日志        ///         /// 
        public void Error(string content)        {            if (LOGLEVEL <= 3)            {                lock (objLock)                {                    WriteLog(content, LogWrite.ERROR);                }            }        }          #endregion         #region Private Methods         /// 
        /// 写日志        ///         /// 
        /// 
        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);            }        }        /// 
        /// 创建工作日志文件        ///         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;        }          /// 
        /// 判断日志文件是否超过预定义最大值        ///         /// 
        /// 
        private bool LogFileIsTooLarge(string fileName)        {            FileInfo fi = new FileInfo(fileName);            if (fi.Length >= FILESIZE)            {                return true;            }            else            {                return false;            }        }          /// 
        /// 处理日志文件(工作日志文件超过最大值)        ///         private void ProcessLogFile()        {            try            {                DirectoryInfo dirInfo = new DirectoryInfo(m_LogDir);                FileInfo[] vFileInfo = dirInfo.GetFiles("*.txt");                if (vFileInfo != null && vFileInfo.Length > LOGFILENUM)                {                    FileInfo oldFile = vFileInfo[0];                    foreach (FileInfo fi in vFileInfo)                    {                        if (DateTime.Compare(fi.LastWriteTime, oldFile.LastWriteTime) < 0)                        {                            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() + " :...  ");

 

转载于:https://www.cnblogs.com/zuochanzi/p/6046896.html

你可能感兴趣的文章
终端安全求生指南(五)-——日志管理
查看>>
Nginx 使用 openssl 的自签名证书
查看>>
创业维艰、守成不易
查看>>
PHP环境安装套件:快速安装LAMP环境
查看>>
CSS3
查看>>
ul下的li浮动,如何是ul有li的高度
查看>>
C++ primer plus
查看>>
python mysqlDB
查看>>
UVALive 3942 Remember the Word Tire+DP
查看>>
Android之HttpClient
查看>>
从微软的DBML文件中我们能学到什么(它告诉了我们什么是微软的重中之重)~目录...
查看>>
被需求搞的一塌糊涂,怎么办?
查看>>
centos 7.2编译安装nginx-1.12.0
查看>>
c_数据结构_队的实现
查看>>
jquery 选择器总结
查看>>
1月10日,11日工作情况
查看>>
Qt设置背景图片
查看>>
Grunt使用心得
查看>>
【阿里云文档】常用文档整理
查看>>
iptables 配置需要保存
查看>>