using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
namespace Pms.UserEvent.Model
{
///
/// 日志级别枚举
///
public enum ELogFileName
{
Web,
Task,
Error,
Com,
Charge
}
/************************************************************************/
/* */
/************************************************************************/
///
/// 日志类
///
public class CLog
{
public static string CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory+ "\\Plugins\\PMS";
private static readonly int FILE_LENGTH = 1024 * 1024;//100K
private static readonly int FILE_BACK_LENGTH = 1024 * 1024 * 5;//1M,可以根据实际情况调整
private static readonly int MAX_NUMBER_FILE = 5;//备份文件的个数+1,为6
private static readonly string strDir = CurrentDirectory + "\\Log";
private static readonly string strBakDir = CurrentDirectory + "\\HisLog";
public List mFileList = new List();
private static Mutex m_MutexSection = new Mutex();
public static CLog log;
static CLog()
{
log = new CLog();
}
///
/// 黑匣子记录的入口
///
/// 日志文件名
/// 要记录的信息
public void WriteLog(string uFileName, string fileContext)
{
uFileName += DateTime.Today.ToString("yyyy-MM-dd");
fileContext = DateTime.Now.ToString() + ": " + fileContext;
DirectoryInfo dir = new DirectoryInfo(strDir);
if (!dir.Exists)
{
CreateDir(strDir);
}
string strFileName = uFileName + ".txt";
string fname = strDir + "\\" + strFileName;
FileInfo finfo = new FileInfo(fname);
if (finfo.Exists && finfo.Length > FILE_LENGTH)
{
//备份文件
m_MutexSection.WaitOne();
DirectoryInfo dirBak = new DirectoryInfo(strBakDir);
if (!dirBak.Exists)
{
CreateDir(strBakDir);
}
///删除该文件
try
{
//做备份处理
#if TEST
#else
string strText = File.ReadAllText(fname);
strText += fileContext;//带上该帧数据
DelBakFile(MAX_NUMBER_FILE, uFileName, strText);
#endif
File.WriteAllText(fname, "");
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
m_MutexSection.ReleaseMutex();
//判断备份件个数是否大于N,大于删除最先一个,再备份
}
else
{
try
{
//如果不存在,创建文件,并写内容
if (!finfo.Exists)
{
CreateMyFile(uFileName, fileContext);
}
//否则的话,追加
else
{
AppendMyText(uFileName, fileContext);
}
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
}
//创建文件
///
/// 创建新的文件
///
/// 文件编号
/// 要记录的信息
private void CreateMyFile(string uFileName, string fileContext)
{
m_MutexSection.WaitOne();
FileInfo fi = new FileInfo(strDir + "\\" + uFileName + ".txt");
//写操作
StreamWriter w = fi.CreateText();
w.WriteLine(fileContext);
w.Close();
m_MutexSection.ReleaseMutex();
}
///
/// 追加信息
///
/// 文件的编号
/// 要记录的信息
private void AppendMyText(string uFileName, string fileContext)
{
m_MutexSection.WaitOne();
try
{
FileInfo fi = new FileInfo(strDir + "\\" + uFileName + ".txt");
StreamWriter w = fi.AppendText();
w.WriteLine(fileContext);
w.Close();
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
m_MutexSection.ReleaseMutex();
}
///
/// 获取系统时间
///
/// 返回系统时间的编码
private string GetSysTime()
{
string timeFileName = "";
timeFileName = DateTime.Now.Year.ToString();
if (int.Parse(DateTime.Now.Month.ToString()) < 10)
{
timeFileName += "0" + DateTime.Now.Month.ToString();
}
else
{
timeFileName += DateTime.Now.Month.ToString();
}
if (int.Parse(DateTime.Now.Day.ToString()) < 10)
{
timeFileName += "0" + DateTime.Now.Day.ToString();
}
else
{
timeFileName += DateTime.Now.Day.ToString();
}
if (int.Parse(DateTime.Now.Hour.ToString()) < 10)
{
timeFileName += "0" + DateTime.Now.Hour.ToString();
}
else
{
timeFileName += DateTime.Now.Hour.ToString();
}
if (int.Parse(DateTime.Now.Minute.ToString()) < 10)
{
timeFileName += "0" + DateTime.Now.Minute.ToString();
}
else
{
timeFileName += DateTime.Now.Minute.ToString();
}
if (int.Parse(DateTime.Now.Second.ToString()) < 10)
{
timeFileName += "0" + DateTime.Now.Second.ToString();
}
else
{
timeFileName += DateTime.Now.Second.ToString();
}
return timeFileName;
}
//创建目录
private void CreateDir(string directory)
{
DirectoryInfo dirInfo = new DirectoryInfo(directory);
dirInfo.Create();
}
//判断备份文件个数是否大于N,大于删除最先一个,再备份
private void DelBakFile(int n, string ID, string strText)
{
mFileList.Clear();
string strPathFile = strBakDir + "\\";
int bakFileNum = GetBakFileNum(ID);
mFileList.Sort();
if (bakFileNum > n)
{
string strMax = strPathFile + mFileList[bakFileNum - 1];
FileInfo finfo = new FileInfo(strMax);
if (finfo.Length > FILE_BACK_LENGTH)
{
strPathFile += mFileList[0];
FileInfo firBakFile = new FileInfo(strPathFile);
firBakFile.Delete();
DelBakFile(n, ID, strText);
}
else
{
strPathFile += mFileList[bakFileNum - 1];
File.AppendAllText(strPathFile, strText);
}
}
else if (bakFileNum == 0)
{
string bakName = strBakDir + "\\";
bakName += GetSysTime();
bakName += "_" + ID;
bakName += "_";
bakName += ".txt";
FileInfo fi = new FileInfo(bakName);
//写操作
StreamWriter w = fi.CreateText();
w.Write(strText);
w.Close();
}
else if (bakFileNum > 0 && bakFileNum <= n)
{
string strMax = strPathFile + mFileList[bakFileNum - 1];
FileInfo finfo = new FileInfo(strMax);
if (finfo.Length > FILE_BACK_LENGTH)
{
string bakName = strBakDir + "\\";
bakName += GetSysTime();
bakName += "_" + ID;
bakName += "_";
bakName += ".txt";
FileInfo firBakFile = new FileInfo(bakName);
StreamWriter w = firBakFile.CreateText();
w.Write(strText);
w.Close();
}
else
{
File.AppendAllText(strMax, strText);
}
}
}
//获得备份文件个数
private int GetBakFileNum(string ID)
{
int n = 0;
DirectoryInfo dirInfo = new DirectoryInfo(strBakDir);
string str = "_" + ID.ToString() + "_";
foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
{
if (fsi is FileInfo && fsi.Extension == ".txt")
{
if (fsi.Name.Contains(str))
{
mFileList.Add(fsi.Name);
n++;
}
}
}
return n;
}
}
}