using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace PCScreenSavers.Common { class Class_Log { /// /// 获取当前版本号 /// public string GetVersion() { string currentVersion = "v1.2.2"; try { string localPath = Directory.GetCurrentDirectory() + "\\ScreenVersion.txt"; FileStream fs = new FileStream(localPath, FileMode.Open); using (StreamReader sr = new StreamReader(fs)) { string temp = sr.ReadLine(); currentVersion = temp.Substring(temp.LastIndexOf("V")); } fs.Close(); } catch { currentVersion = "v1.2.2"; } return currentVersion; } /**/ /// /// 写入日志文件 /// /// public void WriteLogFile(string input,string strfileName="Errlog") { try { /**/ ///指定日志文件的目录 strfileName = strfileName + DateTime.Now.ToString("yyyy-MM-dd"); string fpath = Directory.GetCurrentDirectory() + "\\log\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\"; if (!Directory.Exists(fpath)) { DirectoryInfo directoryInfo = new DirectoryInfo(fpath); directoryInfo.Create(); } string fname = Directory.GetCurrentDirectory() + "\\log\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\" + strfileName + ".txt"; /**/ ///定义文件信息对象 /// FileInfo finfo = new FileInfo(fname); if (!finfo.Exists) { FileStream fs = new FileStream(fname, FileMode.Create, FileAccess.Write); //fs = File.Create(fname); fs.Close(); finfo = new FileInfo(fname); } /**/ ///判断文件是否存在以及是否大于2K if (finfo.Length > 1024 * 1024 * 10) { /**/ ///文件超过10MB则重命名 File.Move(Directory.GetCurrentDirectory() + "\\LogFile.txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\LogFile.txt"); /**/ ///删除该文件 //finfo.Delete(); } //finfo.AppendText(); /**/ ///创建只写文件流 using (FileStream fs = finfo.OpenWrite()) { /**/ ///根据上面创建的文件流创建写数据流 StreamWriter w = new StreamWriter(fs); /**/ ///设置写数据流的起始位置为文件流的末尾 w.BaseStream.Seek(0, SeekOrigin.End); /**/ ///写入“Log Entry : ” w.Write("\n\rLog Entry : "); /**/ ///写入当前系统时间并换行 w.Write("{0} {1} \n\r", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString()); /**/ ///写入日志内容并换行 w.Write(input + "\n\r"); /**/ ///写入------------------------------------“并换行 w.Write("------------------------------------\n\r"); /**/ ///清空缓冲区内容,并把缓冲区内容写入基础流 w.Flush(); /**/ ///关闭写数据流 w.Close(); } } catch{} } /**/ /// /// 写入日志文件 /// nLogType 1:请求包 2:返回报 /// /// public void WriteTradeLogFile(string input, int nLogType,string strfileName = "Trade") { try { /**/ ///指定日志文件的目录 strfileName = strfileName + DateTime.Now.ToString("yyyy-MM-dd"); string fname = Directory.GetCurrentDirectory() + "\\log\\" + strfileName + ".txt"; /**/ ///定义文件信息对象 FileInfo finfo = new FileInfo(fname); if (!finfo.Exists) { FileStream fs; fs = File.Create(fname); fs.Close(); finfo = new FileInfo(fname); } /**/ ///判断文件是否存在以及是否大于2K if (finfo.Length > 1024 * 1024 * 10) { /**/ ///文件超过10MB则重命名 File.Move(Directory.GetCurrentDirectory() + "\\LogFile.txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\LogFile.txt"); /**/ ///删除该文件 //finfo.Delete(); } //finfo.AppendText(); /**/ ///创建只写文件流 using (FileStream fs = finfo.OpenWrite()) { /**/ ///根据上面创建的文件流创建写数据流 StreamWriter w = new StreamWriter(fs); /**/ ///设置写数据流的起始位置为文件流的末尾 w.BaseStream.Seek(0, SeekOrigin.End); /**/ ///写入“Log Entry : ” if (nLogType == 1) w.Write("\n\r请求包 : "); else if (nLogType == 2) w.Write("\n\r返回包 : "); /**/ ///写入当前系统时间并换行 w.Write("{0} {1} \n\r", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString()); /**/ ///写入日志内容并换行 w.Write(input + "\n\r"); /**/ ///写入------------------------------------“并换行 w.Write("------------------------------------\n\r"); /**/ ///清空缓冲区内容,并把缓冲区内容写入基础流 w.Flush(); /**/ ///关闭写数据流 w.Close(); } } catch { } } /// /// 删除 month 个月之前的 strFileName日志 /// /// /// public static void DeleteLogFile(string strFileName = "ErrLog", int month = 1) { try { string logPath = Directory.GetCurrentDirectory() + "\\log\\" + strFileName; if (!Directory.Exists(logPath))//若文件夹不存在则返回 return; else logPath += "\\"; DirectoryInfo theFolder = new DirectoryInfo(@logPath); FileInfo[] fileInfo = theFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { DateTime dt1 = Convert.ToDateTime(NextFile.Name.Substring(strFileName.Length, NextFile.Name.Length - strFileName.Length - 4) + " 00:00:00"); DateTime dt2 = DateTime.Now.AddMonths(-month); if (dt1 < dt2) { NextFile.Delete(); } } } catch (System.Exception) { } } /// /// 删除 fileName 文件夹里一个月之前的日志 /// /// /// public void DeleteFiles(string fileName = "") { try { if (string.IsNullOrEmpty(fileName)) fileName = Directory.GetCurrentDirectory() + "\\log"; if (!Directory.Exists(fileName))//若文件夹不存在则返回 return; DirectoryInfo dir = new DirectoryInfo(fileName); //不是目录 if (dir == null) return; //DateTime DT = dir.CreationTime; FileSystemInfo[] files = dir.GetFileSystemInfos(); for (int i = 0; i < files.Length; i++) { FileInfo file = files[i] as FileInfo; //是文件 if (file != null) { DateTime DT = file.CreationTime; if (DateTime.Now.Month - DT.Month >= 1) { file.Delete(); } } else { DirectoryInfo dir1 = files[i] as DirectoryInfo; if (dir1 != null) { DateTime DT = dir1.CreationTime; if (DateTime.Now.Month - DT.Month >= 1) { //dir1.Delete(); System.IO.Directory.Delete(fileName + "\\" + dir1.Name, true); } } } } } catch { } } /**/ /// /// 写入日志文件 /// /// public void WriteRecordToFile(string programCode,string time) { try { string fpath = Directory.GetCurrentDirectory() + "\\record\\" ; if (!Directory.Exists(fpath)) { DirectoryInfo directoryInfo = new DirectoryInfo(fpath); directoryInfo.Create(); } string fname = Directory.GetCurrentDirectory() + "\\record\\Record.txt"; /**/ ///定义文件信息对象 /// FileInfo finfo = new FileInfo(fname); if (!finfo.Exists) { FileStream fs = new FileStream(fname, FileMode.Create, FileAccess.Write); //fs = File.Create(fname); fs.Close(); finfo = new FileInfo(fname); } //finfo.AppendText(); /**/ ///创建只写文件流 using (FileStream fs = finfo.OpenWrite()) { /**/ ///根据上面创建的文件流创建写数据流 StreamWriter w = new StreamWriter(fs); /**/ ///设置写数据流的起始位置为文件流的末尾 w.BaseStream.Seek(0, SeekOrigin.End); /**/ ///写入“Log Entry : ” /// if (finfo.Length > 10) { w.Write(","); } string wir ="{\"ProgramCode\": \""+programCode+"\",\"Time\": "+time+",\"PlayTime\":\""+DateTime.Now.ToString("G")+"\" }"; w.Write(wir); /**/ ///写入当前系统时间并换行 // w.Write("{0} {1} \n\r", DateTime.Now.ToLongTimeString(),DateTime.Now.ToLongDateString()); /**/ ///写入日志内容并换行 // w.Write(input + "\n\r"); /**/ ///写入------------------------------------“并换行 // w.Write("------------------------------------\n\r"); /**/ ///清空缓冲区内容,并把缓冲区内容写入基础流 w.Flush(); /**/ ///关闭写数据流 w.Close(); } } catch (Exception e) { WriteLogFile(e.Message, "WriteRecordToFileError"); } } } }