using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace CRVM.Utility { /// /// 画面日志实体类 /// public sealed class EntityLog { private string projname = ""; /// /// Gets or sets the name of the proj. /// /// The name of the proj. public string ProjName { get { return projname; } set { projname = value; } } private string information; /// /// Gets or sets the information. /// /// The information. public string Information { get { return information; } set { information = value; } } private string keyInfo=""; public string KeyInfo { get { return keyInfo; } set { keyInfo = value; } } private string otherInfo; /// /// Gets or sets the other info. /// /// The other info. public string OtherInfo { get { return otherInfo; } set { otherInfo = value; } } /// /// Initializes a new instance of the class. /// /// The information. /// The projname. public EntityLog(string information, string projname) { this.information = information; this.projname = projname; } /// /// Initializes a new instance of the class. /// /// The information. /// The projname. /// The otherinfo. public EntityLog(string information, string projname, string otherinfo) { this.information = information; this.projname = projname; this.otherInfo = otherinfo; } /// /// initializes a new instance of < EntityLog > class /// /// the information /// the projname /// the key word, such as 'Error, Info, Start ,End' /// the detail infomation public EntityLog(string information, string projname, string keyInfo, string otherinfo) { this.information = information; this.projname = projname; this.keyInfo = keyInfo; this.otherInfo = otherinfo; } } public sealed class MLog { private const int projnameLen = 10;//项目名称20字符长 private const int infoLen = 38;//项目名称30字符长 private const int keywordLen = 12;//操作结果提示10字符长 private string logfileName; public MLog(string logName) { logfileName = logName; } public int WriteLogFile(string projname, string information) { EntityLog ea = new EntityLog(information, projname); WriteLogFile(ea); return 0; } public int WriteLogFile(string projname, string information, string otherInfo) { EntityLog ea = new EntityLog(information, projname, otherInfo); WriteLogFile(ea); return 1; } public int WriteLogFile(string projname, string information,string keyInfo, string otherInfo) { EntityLog ea = new EntityLog(information, projname, keyInfo, otherInfo); WriteLogFile(ea); return 1; } private int WriteLogFile(EntityLog ealarm) { // lock (this) // { // try // { // StreamWriter write; // DateTime time = System.DateTime.Now; // string path = Application.StartupPath + "\\log\\"; // int actprojLen = ealarm.ProjName.Length; // String srtProj; // if (actprojLen < projnameLen) // srtProj = new String(' ', projnameLen - actprojLen); // else // srtProj = ""; // int actinfoLen = ealarm.Information.Length; // String srtInfo; // if (actinfoLen < infoLen) // srtInfo = new String(' ', infoLen - actinfoLen); // else // srtInfo = ""; // int actkeywordLen = ealarm.KeyInfo.Length; // String strKeyWord; // if (actkeywordLen < keywordLen) // strKeyWord = new string(' ', keywordLen - actkeywordLen); // else // strKeyWord = ""; // if (File.Exists(@path + logfileName + "." + time.ToString("yyyy-MM-dd") + ".log")) // { // write = File.AppendText(@path + logfileName + "." + time.ToString("yyyy-MM-dd") + ".log"); // string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.KeyInfo + strKeyWord + " | " + ealarm.OtherInfo; // write.WriteLine(s); // write.Flush(); // write.Close(); // } // else // { // if (!Directory.Exists(path)) // Directory.CreateDirectory(path); // write = File.CreateText(@path + logfileName + "." + time.ToString("yyyy-MM-dd") + ".log"); // string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.KeyInfo + strKeyWord + " | " + ealarm.OtherInfo; // write.WriteLine(s); // write.Flush(); // write.Close(); // } // return 1; // } // catch (FieldAccessException fex) // { // throw new Exception(fex.Message); // } // } return 1; } } /// /// Project develop use this class for record the operatings database /// public sealed class DevelopLog { private MLog mLog; private static DevelopLog opLog = null; private const string logName = "devlog"; private const int keywordLen = 12;//操作结果提示10字符长 private const int projnameLen = 10;//项目名称20字符长 private const int infoLen = 38;//项目名称30字符长 private DevelopLog(string logName) { mLog = new MLog(logName); } public static DevelopLog DeBug { get { if (opLog == null) opLog = new DevelopLog(logName); return opLog; } } public int WriteLogFile(string projname, string information) { EntityLog ea = new EntityLog(information, projname); WriteLogFile(ea); return 0; } public int WriteLogFile(string projname, string information, string otherInfo) { EntityLog ea = new EntityLog(information, projname, otherInfo); WriteLogFile(ea); return 1; } public int WriteLogFile(string projname, string information, string keyInfo, string otherInfo) { EntityLog ea = new EntityLog(information, projname, keyInfo, otherInfo); WriteLogFile(ea); return 1; } /// /// 根据类型进行选择写日志信息 /// /// /// /// /// /// /// public int WriteLog(string projname, string information, string keyInfo, string otherInfo,string recordType) { if (recordType == "All" || recordType == keyInfo.ToUpper()) { return mLog.WriteLogFile(projname, information, keyInfo, otherInfo); } else return 1; } private int WriteLogFile(EntityLog ealarm) { lock (this) { try { StreamWriter write; DateTime time = System.DateTime.Now; string path = Application.StartupPath + "\\log\\"; int actprojLen = ealarm.Information.Length; String srtProj; if (actprojLen < projnameLen) srtProj = new String(' ', projnameLen - actprojLen); else srtProj = ""; int actinfoLen = ealarm.Information.Length; String srtInfo; if (actinfoLen < infoLen) srtInfo = new String(' ', infoLen - actinfoLen); else srtInfo = ""; int actkeywordLen = ealarm.KeyInfo.Length; String strKeyWord; if (actkeywordLen < keywordLen) strKeyWord = new string(' ', keywordLen - actkeywordLen); else strKeyWord = ""; if (File.Exists(@path + logName + "." + time.ToString("yyyy-MM-dd") + ".log")) { write = File.AppendText(@path + logName + "." + time.ToString("yyyy-MM-dd") + ".log"); string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.KeyInfo + strKeyWord + " | " + ealarm.OtherInfo; write.WriteLine(s); write.Flush(); write.Close(); } else { if (!Directory.Exists(path)) Directory.CreateDirectory(path); write = File.CreateText(@path + logName + "." + time.ToString("yyyy-MM-dd") + ".log"); string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.KeyInfo + strKeyWord + " | " + ealarm.OtherInfo; write.WriteLine(s); write.Flush(); write.Close(); } return 1; } catch (FieldAccessException fex) { throw new Exception(fex.Message); } } } } #region 原来的代码 ///// ///// 画面日志实体类 ///// //public sealed class EntityLog //{ // private string projname = ""; // /// // /// Gets or sets the name of the proj. // /// // /// The name of the proj. // public string ProjName // { // get { return projname; } // set { projname = value; } // } // private string information; // /// // /// Gets or sets the information. // /// // /// The information. // public string Information // { // get { return information; } // set { information = value; } // } // private string otherInfo; // /// // /// Gets or sets the other info. // /// // /// The other info. // public string OtherInfo // { // get { return otherInfo; } // set { otherInfo = value; } // } // /// // /// Initializes a new instance of the class. // /// // /// The information. // /// The projname. // public EntityLog(string information,string projname) // { // this.information = information; // this.projname = projname; // } // /// // /// Initializes a new instance of the class. // /// // /// The information. // /// The projname. // /// The otherinfo. // public EntityLog(string information, string projname, string otherinfo) // { // this.information = information; // this.projname = projname; // this.otherInfo = otherinfo; // } //} //public sealed class MLog //{ // private const int projnameLen = 15;//项目名称20字符长 // private const int infoLen = 50;//项目名称30字符长 // private string logfileName; // public MLog(string logName) // { // logfileName = logName; // } // public int WriteLogFile(string projname,string information) // { // EntityLog ea = new EntityLog(information, projname); // WriteLogFile(ea); // return 0; // } // public int WriteLogFile(string projname, string information, string otherInfo) // { // EntityLog ea = new EntityLog(information, projname, otherInfo); // WriteLogFile(ea); // return 1; // } // private int WriteLogFile(EntityLog ealarm) // { // try // { // StreamWriter write; // DateTime time = System.DateTime.Now; // string path = Application.StartupPath + "\\log\\"; // int actprojLen = ealarm.ProjName.Length; // String srtProj; // if (actprojLen < projnameLen) // srtProj = new String(' ', projnameLen - actprojLen); // else // srtProj = ""; // int actinfoLen = ealarm.Information.Length; // String srtInfo; // if (actinfoLen < infoLen) // srtInfo = new String(' ', infoLen - actinfoLen); // else // srtInfo = ""; // if (File.Exists(@path+ logfileName + "." + time.ToString("yyyy-MM-dd") + ".log")) // { // write = File.AppendText(@path + logfileName + "." + time.ToString("yyyy-MM-dd") + ".log"); // string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.OtherInfo; // write.WriteLine(s); // write.Flush(); // write.Close(); // } // else // { // if (!Directory.Exists(path)) // Directory.CreateDirectory(path); // write = File.CreateText(@path + logfileName + "." + time.ToString("yyyy-MM-dd") + ".log"); // string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.OtherInfo; // write.WriteLine(s); // write.Flush(); // write.Close(); // } // return 1; // } // catch (FieldAccessException fex) // { // throw new Exception(fex.Message); // } // } //} ///// ///// Project develop use this class for record the operatings database ///// //public sealed class DevelopLog //{ // private MLog mLog; // private static DevelopLog opLog = null; // private const string logName = "devlog"; // private DevelopLog(string logName) // { // mLog = new MLog(logName); // } // public static DevelopLog DeBug // { // get // { // if (opLog == null) // opLog = new DevelopLog(logName); // return opLog; // } // } // public int WriteLog(string projname, string information) // { // return mLog.WriteLogFile(projname, information); // } // public int WriteLog(string projname, string information, string otherInfo) // { // return mLog.WriteLogFile(projname, information, otherInfo); // } //} #endregion }