using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace HXX.Scanner.Copier { /// /// 日志类 /// public class LogManager { public static readonly string ServerLogPath = @"C:\HXX\"; public static readonly string ServerLogExtension = "-Log.txt"; public static readonly string ServerLogDateFormat = "yyyy-MM-dd"; private static DateTime CurrentDate = DateTime.Now.Date; private static string ServerLogFileName = string.Empty; public static void WriteLog(string Message) { WriteLog(Message, null); } public static void WriteLog(Exception ee) { WriteLog(ee.Message, ee); } public static void WriteLog(string Message, Exception ee) { try { CheckLogFile(); using (StreamWriter sw = new StreamWriter(ServerLogFileName, true)) { string Content = string.Empty; Content += "=======================================================================================================" + Environment.NewLine; Content += "************** Time At:" + DateTime.Now.ToString(@"[yyyy-MM-dd HH:mm:ss]") + Environment.NewLine; Content += "************** Description:" + Environment.NewLine + Message + Environment.NewLine; if (ee != null) { Content += ee.Message + Environment.NewLine; Content += "************** Error Stack Trace:" + Environment.NewLine + ee.StackTrace + Environment.NewLine; Content += "************** Error Source:" + ee.Source + Environment.NewLine; } Content += "=======================================================================================================" + Environment.NewLine; Content += Environment.NewLine; Content += Environment.NewLine; sw.Write(Content); } } catch { } } private static void CheckLogFile() { if (CurrentDate != DateTime.Now.Date || ServerLogFileName == string.Empty) { CurrentDate = DateTime.Now.Date; ServerLogFileName = DateTime.Now.ToString(ServerLogDateFormat) + ServerLogExtension; ServerLogFileName = ServerLogPath + ServerLogFileName; } if (!Directory.Exists(ServerLogPath)) { Directory.CreateDirectory(ServerLogPath); } } public static void WriteLogCoco(string Title, List ExceptionList) { try { CheckLogFile(); using (StreamWriter sw = new StreamWriter(ServerLogFileName, true)) { string Content = string.Empty; Content += "=======================================================================================================" + Environment.NewLine; Content += "************** Time At:" + DateTime.Now.ToString(@"[yyyy-MM-dd HH:mm:ss]") + Environment.NewLine; Content += Title + Environment.NewLine; foreach (Exception ee in ExceptionList) { Content += "************** Description:" + Environment.NewLine + ee.Message + Environment.NewLine; if (ee != null) { Content += "************** Error Stack Trace:" + Environment.NewLine + ee.StackTrace + Environment.NewLine; Content += "************** Error Source:" + ee.Source + Environment.NewLine + Environment.NewLine; } } Content += "=======================================================================================================" + Environment.NewLine; Content += Environment.NewLine; Content += Environment.NewLine; sw.Write(Content); } } catch { } } } }