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() + " :... ");