static bool logger(LogerType type, string title, string msg) { LogerManager loger = new LogerManager(); loger.type = type; loger.title = title; loger.msg = msg; UnLog.Enqueue(loger); StartLogin(); return(true); }
/// <summary> /// 开始记录日志 /// </summary> public static void StartLogin() { if (IsWork) { return; } IsWork = true; Task.Run(() => { while (IsWork) { try { if (UnLog.Count == 0) { IsWork = false; return; } Thread.Sleep(1000); LogerManager loger = UnLog.Dequeue() as LogerManager; if (loger != null) { string filePath, fileName; if (!IsServer) { filePath = LogPath + "\\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString(); fileName = filePath + "\\" + DateTime.Now.Day.ToString() + ".Log"; } else { filePath = LogPath; fileName = Path.Combine(filePath, DateTime.Now.Day.TryToString() + ".Log"); } if (!System.IO.Directory.Exists(filePath)) { System.IO.Directory.CreateDirectory(filePath); } FileStream filestream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); StreamWriter writer = new StreamWriter(filestream, System.Text.Encoding.Default); writer.BaseStream.Seek(0, SeekOrigin.End); writer.WriteLine("{0} >{1}> {2} > {3}", DateTime.Now.TimeOfDay, loger.type.TryToString(), loger.title, loger.msg); writer.Flush(); writer.Close(); filestream.Close(); } } catch { } } }); }