using System; using System.Text; using System.IO; using Sog.Log; namespace MysqlHandling { class EasyLog { public static FileInfo m_fileInfo; public static object locker = new object(); private static DateTime m_dateTime1970_utc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); private static DateTime m_dateTime1970 = m_dateTime1970_utc.ToLocalTime(); public static void WriteInfo(string logStr) { lock (locker) { //Console.WriteLine(logStr); logStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " INFO : " + logStr + "\r\n"; File.AppendAllText(m_fileInfo.FullName, logStr); /* using (FileStream fs = m_fileInfo.Open(FileMode.Append, FileAccess.Write)) { Byte[] info = new UTF8Encoding(true).GetBytes(logStr); fs.Write(info, 0, info.Length); }*/ } } public static void WriteErr(string logStr) { lock (locker) { logStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ERROR : " + logStr + "\r\n"; using (FileStream fs = m_fileInfo.Open(FileMode.Append, FileAccess.Write)) { var writer = new StreamWriter(fs); writer.Write(logStr); } } } public static void WriteWarinig(string logStr) { lock (locker) { logStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " WARNING: " + logStr + "\r\n"; using (FileStream fs = m_fileInfo.Open(FileMode.Append, FileAccess.Write)) { var writer = new StreamWriter(fs); writer.Write(logStr); } } } public static void WriteDebug(string logStr) { logStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " DEBUG : " + logStr + "\r\n"; lock (locker) { using (FileStream fs = m_fileInfo.Open(FileMode.Append, FileAccess.Write)) { var writer = new StreamWriter(fs); writer.Write(logStr); } } } //这个等价于GetTime(DateTime t) public static long ConvertDateTimeToUnixTime(DateTime datatime) { TimeSpan ts = datatime - m_dateTime1970; long timeMs = (long)ts.TotalMilliseconds; return timeMs; } public static void CreatLogFile(string path, string fileName) { string fullPath = path + "/" + fileName; m_fileInfo = new FileInfo(fullPath); if (m_fileInfo.Exists) { return; } using (m_fileInfo.Create()) { Console.WriteLine("Create Sucessed"); } } } }