using HotelPms.Share.Util; using System; using System.Globalization; using System.IO; using System.Text; using System.Threading; namespace HotelPms.Share.IO { public class OperationLog { /// ****************************** Description ******************************* /// žƒVƒXƒeƒ€–¼Ì /// @”Ä—pƒNƒ‰ƒX /// žŠT—v /// @ŠÈ’POpeLog”­s /// ž—š—ð /// @2016/02/28 ¬–؁@Ÿ—´ V‹Kì¬ /// ****************************** Declarations ****************************** #region ššššš@Declartions@ššššš public enum LogLevelType : int { Normal = 0, Level1, Level2, Level3, Level4, Level5, Level6, /// /// ’ʏíSQLƒƒO /// Level7, /// /// ƒ^ƒCƒ}[‚̏„‰ñˆ—SQLƒƒO /// Level8, /// /// DB‚Ìnew MsSqlNetƒŒƒxƒ‹ /// Level9, } private static string LogLevelFile = System.AppDomain.CurrentDomain.BaseDirectory + @"LogLevel.dat"; private static OperationLog? m_Default; public static object SyncRoot = new object(); private string rootPath = System.AppDomain.CurrentDomain.BaseDirectory + @"OpeLog" + Path.DirectorySeparatorChar; private long m_FileMaxSize = 512; //’PˆÊFKB private int m_FileIndex = 1; private bool m_Enabled = true; public event MessageEventHandler? OutPut; #endregion #region ššššš@Property@ššššš private LogLevelType m_Level = LogLevelType.Normal; public LogLevelType Level { get { return m_Level; } set { m_Level = value; } } public static OperationLog Instance { get { if (m_Default == null) { m_Default = new OperationLog(System.AppDomain.CurrentDomain.BaseDirectory + @"OpeLog" + Path.DirectorySeparatorChar + Environment.MachineName + "_" + Environment.UserName + Path.DirectorySeparatorChar, 512); } return m_Default; } } public long FileMaxSize { set { m_FileMaxSize = value; } get { return m_FileMaxSize; } } /// /// Log‚ðŽæ‚é‚©‚Ç‚¤‚© /// public string RootPath { get { return rootPath; } set { rootPath = value; } } public bool Enabled { get { return m_Enabled; } set { m_Enabled = value; } } #endregion #region ššššš@Class Event@ššššš /// /// ƒRƒ“ƒXƒgƒ‰ƒNƒ^ /// /// ‹@”\ID‘Š“–(’ʏíSystemLog‚ª’è‹`‚³‚ê‚Ä‚¢‚鉿–ʁAƒNƒ‰ƒX) /// –¾Ž¦“I‚ɃƒOŽæ‚è‚ð‚·‚é‚©‚Ç‚¤‚© /// /// ’PˆÊFKB public OperationLog(string rootPath, long maxSize) { if (rootPath.Length > 0) { this.rootPath = rootPath; } this.m_FileMaxSize = maxSize; if (File.Exists(LogLevelFile)) { m_Level = (LogLevelType)CConvert.ToInt(File.ReadAllText(LogLevelFile, Encoding.UTF8)); } InitLogger(); } #endregion #region ššššš@Public Functions@ššššš /// /// ƒVƒXƒeƒ€ŽžŠÔ‚©‚çdelayMonthŒŽ‘O‚̃tƒHƒ‹ƒ_‚ðíœ‚·‚é /// /// public void ClearLog(int delayMonth) { try { string[] dirList = Directory.GetDirectories(rootPath); foreach (string curDir in dirList) { if (curDir.Substring(curDir.Length - 6, 6).CompareTo(DateTime.Now.Date.AddMonths(-delayMonth).ToString("yyyyMM")) <= 0) { FileOperation.DeleteDir(curDir); } } } catch { } } /// /// ƒVƒXƒeƒ€ŽžŠÔ‚©‚çdelayDay“ú‘O‚̃tƒHƒ‹ƒ_‚ƃtƒ@ƒCƒ‹‚ðíœ‚·‚é /// /// public void ClearLogByDay(int delayDay) { try { string beginYM = DateTime.Now.Date.AddDays(-delayDay).ToString("yyyyMM"); int beginD = CConvert.ToInt(DateTime.Now.Date.AddDays(-delayDay).ToString("dd")); string[] dirList = Directory.GetDirectories(rootPath); foreach (string curDir in dirList) { if (curDir.Substring(curDir.Length - 6, 6).CompareTo(beginYM) < 0) { FileOperation.DeleteDir(curDir); } else if (curDir.Substring(curDir.Length - 6, 6).CompareTo(beginYM) == 0) { //“ú•t‚ðíœ string[] fileList = Directory.GetFiles(curDir); foreach (string file in fileList) { if (CConvert.ToInt(Path.GetFileNameWithoutExtension(file).Substring(0, 2)) < beginD) { FileOperation.Delete(file); } } } } } catch { } } #endregion #region ššššš@Private Functions@ššššš private void RaiseOutputEvent(string msg) { if (OutPut != null) { OutPut(this, new MessageEventArgs(msg)); } } private string GetFileName() { if (m_FileMaxSize == 0) { return DateTime.Now.Day.ToString("00", NumberFormatInfo.CurrentInfo); } string dir = rootPath + DateTime.Now.ToString("yyyyMM"); string fileBase = DateTime.Now.Day.ToString("00", NumberFormatInfo.CurrentInfo) + "_{0}"; while (true) { string fileName = string.Format(fileBase, m_FileIndex); string fileFullPath = dir + Path.DirectorySeparatorChar + fileName + ".log"; if (!File.Exists(fileFullPath)) { File.WriteAllText(fileFullPath, "ˆ—ŽžŠÔ,“à—e" + Environment.NewLine, Encoding.UTF8); return fileName; } FileInfo fileInfo = new FileInfo(fileFullPath); if (fileInfo.Length < (m_FileMaxSize * 1024)) { return fileName; } RaiseOutputEvent("GetFileNameF" + fileFullPath); m_FileIndex++; } } /// /// logger‰Šú‰» /// private void InitLogger() { try { string dir = rootPath + DateTime.Now.ToString("yyyyMM"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); m_FileIndex = 1; } string filePath = dir + Path.DirectorySeparatorChar + GetFileName() + ".log"; if (!File.Exists(filePath)) { File.WriteAllText(filePath, "ˆ—ŽžŠÔ,“à—e" + Environment.NewLine, Encoding.UTF8); } //this.projectName = Assembly.GetCallingAssembly().ManifestModule.Name; } catch (Exception ex) { RaiseOutputEvent("InitLoggerF" + ex.Message); } } public void WriteLog(string message) { WriteLog(message, LogLevelType.Normal); } public void WriteLog(string message, LogLevelType level) { if (!m_Enabled) { return; } if ((int)m_Level < (int)level) { return; } lock (SyncRoot) { InitLogger(); //ŒŽ•Ï‚í‚Á‚½Žž‚É try { string filePath = rootPath + DateTime.Now.ToString("yyyyMM") + Path.DirectorySeparatorChar + GetFileName() + ".log"; using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8)) { sw.WriteLine(string.Format("y{0}z{1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss fff"), message)); sw.Close(); } } catch (Exception ex) { RaiseOutputEvent("WriteLogF" + ex.Message); } } } public static void Write(string msg) { Write(msg, LogLevelType.Normal); } public static void Write(string msg, LogLevelType level) { try { //”ñ“¯Šú•ûŽ® ParameterizedThreadStart theradStart = new ParameterizedThreadStart(delegate(object data) { LogInfo item = data as LogInfo; OperationLog.Instance.WriteLog(item.Message, item.Level); }); Thread tr = new Thread(theradStart); tr.Start(new LogInfo(msg, level)); } catch { return; } } public static void Clear(int delayMonth) { try { //”ñ“¯Šú•ûŽ® ParameterizedThreadStart theradStart = new ParameterizedThreadStart(delegate(object data) { OperationLog.Instance.ClearLog(int.Parse(data.ToString())); }); Thread tr = new Thread(theradStart); tr.Start(delayMonth); } catch { return; } } #endregion } }