CLog.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace Pms.UserEvent.Model
  6. {
  7. /// <summary>
  8. /// 日志级别枚举
  9. /// </summary>
  10. public enum ELogFileName
  11. {
  12. Web,
  13. Task,
  14. Error,
  15. Com,
  16. Charge
  17. }
  18. /************************************************************************/
  19. /* */
  20. /************************************************************************/
  21. /// <summary>
  22. /// 日志类
  23. /// </summary>
  24. public class CLog
  25. {
  26. public static string CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory+ "\\Plugins\\PMS";
  27. private static readonly int FILE_LENGTH = 1024 * 1024;//100K
  28. private static readonly int FILE_BACK_LENGTH = 1024 * 1024 * 5;//1M,可以根据实际情况调整
  29. private static readonly int MAX_NUMBER_FILE = 5;//备份文件的个数+1,为6
  30. private static readonly string strDir = CurrentDirectory + "\\Log";
  31. private static readonly string strBakDir = CurrentDirectory + "\\HisLog";
  32. public List<string> mFileList = new List<string>();
  33. private static Mutex m_MutexSection = new Mutex();
  34. public static CLog log;
  35. static CLog()
  36. {
  37. log = new CLog();
  38. }
  39. /// <summary>
  40. /// 黑匣子记录的入口
  41. /// </summary>
  42. /// <param name="uFileName">日志文件名</param>
  43. /// <param name="fileContext">要记录的信息</param>
  44. public void WriteLog(string uFileName, string fileContext)
  45. {
  46. uFileName += DateTime.Today.ToString("yyyy-MM-dd");
  47. fileContext = DateTime.Now.ToString() + ": " + fileContext;
  48. DirectoryInfo dir = new DirectoryInfo(strDir);
  49. if (!dir.Exists)
  50. {
  51. CreateDir(strDir);
  52. }
  53. string strFileName = uFileName + ".txt";
  54. string fname = strDir + "\\" + strFileName;
  55. FileInfo finfo = new FileInfo(fname);
  56. if (finfo.Exists && finfo.Length > FILE_LENGTH)
  57. {
  58. //备份文件
  59. m_MutexSection.WaitOne();
  60. DirectoryInfo dirBak = new DirectoryInfo(strBakDir);
  61. if (!dirBak.Exists)
  62. {
  63. CreateDir(strBakDir);
  64. }
  65. ///删除该文件
  66. try
  67. {
  68. //做备份处理
  69. #if TEST
  70. #else
  71. string strText = File.ReadAllText(fname);
  72. strText += fileContext;//带上该帧数据
  73. DelBakFile(MAX_NUMBER_FILE, uFileName, strText);
  74. #endif
  75. File.WriteAllText(fname, "");
  76. }
  77. catch (Exception ex)
  78. {
  79. Console.Write(ex.ToString());
  80. }
  81. m_MutexSection.ReleaseMutex();
  82. //判断备份件个数是否大于N,大于删除最先一个,再备份
  83. }
  84. else
  85. {
  86. try
  87. {
  88. //如果不存在,创建文件,并写内容
  89. if (!finfo.Exists)
  90. {
  91. CreateMyFile(uFileName, fileContext);
  92. }
  93. //否则的话,追加
  94. else
  95. {
  96. AppendMyText(uFileName, fileContext);
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. Console.Write(ex.ToString());
  102. }
  103. }
  104. }
  105. //创建文件
  106. /// <summary>
  107. /// 创建新的文件
  108. /// </summary>
  109. /// <param name="uFileName">文件编号</param>
  110. /// <param name="fileContext">要记录的信息</param>
  111. private void CreateMyFile(string uFileName, string fileContext)
  112. {
  113. m_MutexSection.WaitOne();
  114. FileInfo fi = new FileInfo(strDir + "\\" + uFileName + ".txt");
  115. //写操作
  116. StreamWriter w = fi.CreateText();
  117. w.WriteLine(fileContext);
  118. w.Close();
  119. m_MutexSection.ReleaseMutex();
  120. }
  121. /// <summary>
  122. /// 追加信息
  123. /// </summary>
  124. /// <param name="uFileName">文件的编号</param>
  125. /// <param name="fileContext">要记录的信息</param>
  126. private void AppendMyText(string uFileName, string fileContext)
  127. {
  128. m_MutexSection.WaitOne();
  129. try
  130. {
  131. FileInfo fi = new FileInfo(strDir + "\\" + uFileName + ".txt");
  132. StreamWriter w = fi.AppendText();
  133. w.WriteLine(fileContext);
  134. w.Close();
  135. }
  136. catch (Exception ex)
  137. {
  138. Console.Write(ex.ToString());
  139. }
  140. m_MutexSection.ReleaseMutex();
  141. }
  142. /// <summary>
  143. /// 获取系统时间
  144. /// </summary>
  145. /// <returns>返回系统时间的编码</returns>
  146. private string GetSysTime()
  147. {
  148. string timeFileName = "";
  149. timeFileName = DateTime.Now.Year.ToString();
  150. if (int.Parse(DateTime.Now.Month.ToString()) < 10)
  151. {
  152. timeFileName += "0" + DateTime.Now.Month.ToString();
  153. }
  154. else
  155. {
  156. timeFileName += DateTime.Now.Month.ToString();
  157. }
  158. if (int.Parse(DateTime.Now.Day.ToString()) < 10)
  159. {
  160. timeFileName += "0" + DateTime.Now.Day.ToString();
  161. }
  162. else
  163. {
  164. timeFileName += DateTime.Now.Day.ToString();
  165. }
  166. if (int.Parse(DateTime.Now.Hour.ToString()) < 10)
  167. {
  168. timeFileName += "0" + DateTime.Now.Hour.ToString();
  169. }
  170. else
  171. {
  172. timeFileName += DateTime.Now.Hour.ToString();
  173. }
  174. if (int.Parse(DateTime.Now.Minute.ToString()) < 10)
  175. {
  176. timeFileName += "0" + DateTime.Now.Minute.ToString();
  177. }
  178. else
  179. {
  180. timeFileName += DateTime.Now.Minute.ToString();
  181. }
  182. if (int.Parse(DateTime.Now.Second.ToString()) < 10)
  183. {
  184. timeFileName += "0" + DateTime.Now.Second.ToString();
  185. }
  186. else
  187. {
  188. timeFileName += DateTime.Now.Second.ToString();
  189. }
  190. return timeFileName;
  191. }
  192. //创建目录
  193. private void CreateDir(string directory)
  194. {
  195. DirectoryInfo dirInfo = new DirectoryInfo(directory);
  196. dirInfo.Create();
  197. }
  198. //判断备份文件个数是否大于N,大于删除最先一个,再备份
  199. private void DelBakFile(int n, string ID, string strText)
  200. {
  201. mFileList.Clear();
  202. string strPathFile = strBakDir + "\\";
  203. int bakFileNum = GetBakFileNum(ID);
  204. mFileList.Sort();
  205. if (bakFileNum > n)
  206. {
  207. string strMax = strPathFile + mFileList[bakFileNum - 1];
  208. FileInfo finfo = new FileInfo(strMax);
  209. if (finfo.Length > FILE_BACK_LENGTH)
  210. {
  211. strPathFile += mFileList[0];
  212. FileInfo firBakFile = new FileInfo(strPathFile);
  213. firBakFile.Delete();
  214. DelBakFile(n, ID, strText);
  215. }
  216. else
  217. {
  218. strPathFile += mFileList[bakFileNum - 1];
  219. File.AppendAllText(strPathFile, strText);
  220. }
  221. }
  222. else if (bakFileNum == 0)
  223. {
  224. string bakName = strBakDir + "\\";
  225. bakName += GetSysTime();
  226. bakName += "_" + ID;
  227. bakName += "_";
  228. bakName += ".txt";
  229. FileInfo fi = new FileInfo(bakName);
  230. //写操作
  231. StreamWriter w = fi.CreateText();
  232. w.Write(strText);
  233. w.Close();
  234. }
  235. else if (bakFileNum > 0 && bakFileNum <= n)
  236. {
  237. string strMax = strPathFile + mFileList[bakFileNum - 1];
  238. FileInfo finfo = new FileInfo(strMax);
  239. if (finfo.Length > FILE_BACK_LENGTH)
  240. {
  241. string bakName = strBakDir + "\\";
  242. bakName += GetSysTime();
  243. bakName += "_" + ID;
  244. bakName += "_";
  245. bakName += ".txt";
  246. FileInfo firBakFile = new FileInfo(bakName);
  247. StreamWriter w = firBakFile.CreateText();
  248. w.Write(strText);
  249. w.Close();
  250. }
  251. else
  252. {
  253. File.AppendAllText(strMax, strText);
  254. }
  255. }
  256. }
  257. //获得备份文件个数
  258. private int GetBakFileNum(string ID)
  259. {
  260. int n = 0;
  261. DirectoryInfo dirInfo = new DirectoryInfo(strBakDir);
  262. string str = "_" + ID.ToString() + "_";
  263. foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
  264. {
  265. if (fsi is FileInfo && fsi.Extension == ".txt")
  266. {
  267. if (fsi.Name.Contains(str))
  268. {
  269. mFileList.Add(fsi.Name);
  270. n++;
  271. }
  272. }
  273. }
  274. return n;
  275. }
  276. }
  277. }