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 *******************************
|
/// VXe¼Ì
|
/// @ÄpNX
|
/// Tv
|
/// @ÈPOpeLogs
|
/// ð
|
/// @2016/02/28 ¬Ø@´ VKì¬
|
/// ****************************** Declarations ******************************
|
|
#region @Declartions@
|
|
public enum LogLevelType : int
|
{
|
Normal = 0,
|
Level1,
|
Level2,
|
Level3,
|
Level4,
|
Level5,
|
Level6,
|
/// <summary>
|
/// ÊíSQLO
|
/// </summary>
|
Level7,
|
/// <summary>
|
/// ^C}[ÌñSQLO
|
/// </summary>
|
Level8,
|
/// <summary>
|
/// DBÌnew MsSqlNetx
|
/// </summary>
|
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; }
|
}
|
|
/// <summary>
|
/// Logðæé©Ç¤©
|
/// </summary>
|
public string RootPath
|
{
|
get { return rootPath; }
|
set { rootPath = value; }
|
}
|
|
public bool Enabled
|
{
|
get { return m_Enabled; }
|
set { m_Enabled = value; }
|
}
|
|
#endregion
|
|
#region @Class Event@
|
|
/// <summary>
|
/// RXgN^
|
/// </summary>
|
/// <param name="currentType">@\ID(ÊíSystemLogªè`³êÄ¢éæÊANX)</param>
|
/// <param name="running">¾¦IÉOæèð·é©Ç¤©</param>
|
/// <param name="rootPath"></param>
|
/// <param name="maxSize">PÊFKB</param>
|
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@
|
|
/// <summary>
|
/// VXeÔ©çdelayMonthOÌtH_ðí·é
|
/// </summary>
|
/// <param name="delayMonth"></param>
|
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
|
{
|
}
|
}
|
|
/// <summary>
|
/// VXeÔ©çdelayDayúOÌtH_Æt@Cðí·é
|
/// </summary>
|
/// <param name="delayMonth"></param>
|
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("GetFileNameF" + fileFullPath);
|
m_FileIndex++;
|
}
|
}
|
|
/// <summary>
|
/// loggerú»
|
/// </summary>
|
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("InitLoggerF" + 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("WriteLogF" + 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
|
}
|
}
|