using System; using System.Collections.Generic; using System.Text; using System.IO; namespace PayService { public class LogHelper { public static string path = System.AppDomain.CurrentDomain.BaseDirectory; public static void WriteServiceLog(string LogData) { LogData = DateTime.Now.ToLongTimeString()+":" + LogData; string LogName = "日志_Send"+System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString() + "-" + System.DateTime.Now.Day.ToString()+".log"; string filename = path + "\\" + LogName; FileStream fs = new FileStream(filename, FileMode.OpenOrCreate); fs.Seek(0, SeekOrigin.End); byte[] _data = new UTF8Encoding().GetBytes(LogData+"\r\n"); fs.Write(_data, 0, _data.Length); fs.Flush(); fs.Close(); MoveFiles("日志_Send*", "Log", -5); } public static void WriteServiceLog2(string LogData) { LogData = DateTime.Now.ToLongTimeString() + ":" + LogData; string LogName = "日志_Receive" + System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString() + "-" + System.DateTime.Now.Day.ToString() + ".log"; string filename = path + "\\" + LogName; FileStream fs = new FileStream(filename, FileMode.OpenOrCreate); fs.Seek(0, SeekOrigin.End); byte[] _data = new UTF8Encoding().GetBytes(LogData + "\r\n"); fs.Write(_data, 0, _data.Length); fs.Flush(); fs.Close(); MoveFiles("日志_Receive*", "Log", -5); } public static void MoveFiles(string FileName, string FilePath = "Log", int DeleteDays = -30, string MovePath = "") { MovePath = MovePath == "" ? path : MovePath; if (!Directory.Exists(MovePath + "\\" + FilePath)) { Directory.CreateDirectory(MovePath + "\\" + FilePath); } DirectoryInfo _DirectoryInfo = new DirectoryInfo(MovePath); foreach (FileInfo _FileInfo in _DirectoryInfo.GetFiles(FileName, SearchOption.TopDirectoryOnly)) { if (_FileInfo.LastWriteTime < DateTime.Now.Date.AddDays(DeleteDays)) { if (File.Exists(MovePath + "\\" + FilePath + "\\" + _FileInfo.Name)) { File.Delete(MovePath + "\\" + FilePath + "\\" + _FileInfo.Name); } _FileInfo.MoveTo(MovePath + "\\" + FilePath + "\\" + _FileInfo.Name); //_FileInfo.Delete(); } } } } }