LogFileOperation.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:LogFileOperation.cs
  5. * 2.功能描述:日志文件操作类
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 张国印 2014/09/01 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using System.IO;
  14. using System.Configuration;
  15. namespace Dongke.IBOSS.PRD.Basics.DataAccess
  16. {
  17. /// <summary>
  18. /// 日志文件操作类
  19. /// </summary>
  20. public static class LogFileOperation
  21. {
  22. #region 对外使用的公用方法
  23. /// <summary>
  24. /// 以Debug级别输出日志信息
  25. /// </summary>
  26. /// <param name="p_fileName">日志文件名称</param>
  27. /// <param name="p_logType">日志类型 true 记录日志,False 不记录日志</param>
  28. /// <param name="p_info">要输出的信息</param>
  29. public static void Debug(string p_fileName, bool p_logType, string p_info)
  30. {
  31. try
  32. {
  33. if (p_logType)
  34. {
  35. if (!string.IsNullOrEmpty(p_fileName))
  36. {
  37. string strDateTime = GetNowDateTime();
  38. string strFileFullName = GetCreateFile(p_fileName);
  39. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  40. {
  41. sw.WriteLine(strDateTime + "(Debug) - " + p_info);
  42. sw.Close();
  43. }
  44. }
  45. }
  46. }
  47. catch { }
  48. }
  49. /// <summary>
  50. /// 以Info级别输出日志信息
  51. /// </summary>
  52. /// <param name="p_fileName">日志文件名称</param>
  53. /// <param name="p_logType">日志类型 true 记录日志,False 不记录日志</param>
  54. /// <param name="p_info">要输出的信息</param>
  55. public static void Info(string p_fileName, bool p_logType, string p_info)
  56. {
  57. try
  58. {
  59. if (p_logType)
  60. {
  61. if (!string.IsNullOrEmpty(p_fileName))
  62. {
  63. string strDateTime = GetNowDateTime();
  64. string strFileFullName = GetCreateFile(p_fileName);
  65. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  66. {
  67. sw.WriteLine(strDateTime + "(Info) - " + p_info);
  68. sw.Close();
  69. }
  70. }
  71. }
  72. }
  73. catch { }
  74. }
  75. /// <summary>
  76. /// 以Warn级别输出日志信息
  77. /// </summary>
  78. /// <param name="p_fileName">日志文件名称</param>
  79. /// <param name="p_logType">日志类型 true 记录日志,False 不记录日志</param>
  80. /// <param name="p_info">要输出的信息</param>
  81. public static void Warn(string p_fileName, bool p_logType, string p_info)
  82. {
  83. try
  84. {
  85. if (p_logType)
  86. {
  87. if (!string.IsNullOrEmpty(p_fileName))
  88. {
  89. string strDateTime = GetNowDateTime();
  90. string strFileFullName = GetCreateFile(p_fileName);
  91. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  92. {
  93. sw.WriteLine(strDateTime + "(Warn) - " + p_info);
  94. sw.Close();
  95. }
  96. }
  97. }
  98. }
  99. catch { }
  100. }
  101. /// <summary>
  102. /// 以Error级别输出日志信息
  103. /// </summary>
  104. /// <param name="p_fileName">日志文件名称</param>
  105. /// <param name="p_info">要输出的信息</param>
  106. public static void Error(string p_fileName, string p_info)
  107. {
  108. try
  109. {
  110. if (!string.IsNullOrEmpty(p_fileName))
  111. {
  112. string strDateTime = GetNowDateTime();
  113. string strFileFullName = GetCreateFile(p_fileName);
  114. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  115. {
  116. sw.WriteLine(strDateTime + "(Error) - " + p_info);
  117. sw.Close();
  118. }
  119. }
  120. }
  121. catch { }
  122. }
  123. /// <summary>
  124. /// 以Fatal级别输出日志信息
  125. /// </summary>
  126. /// <param name="p_fileName">日志文件名称</param>
  127. /// <param name="p_info">要输出的信息</param>
  128. public static void Fatal(string p_fileName, string p_info)
  129. {
  130. try
  131. {
  132. if (!string.IsNullOrEmpty(p_fileName))
  133. {
  134. string strDateTime = GetNowDateTime();
  135. string strFileFullName = GetCreateFile(p_fileName);
  136. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  137. {
  138. sw.WriteLine(strDateTime + "(Fatal) - " + p_info);
  139. sw.Close();
  140. }
  141. }
  142. }
  143. catch { }
  144. }
  145. /// <summary>
  146. /// 创建SQL语句,覆盖文件
  147. /// </summary>
  148. /// <param name="p_fileName">文件名</param>
  149. /// <param name="p_sql">要保存的SQL语句</param>
  150. public static void CreateSqlFile(string p_fileName, List<string> p_sql)
  151. {
  152. try
  153. {
  154. if (!string.IsNullOrEmpty(p_fileName))
  155. {
  156. DeleteFile(p_fileName);
  157. string strFileFullName = GetCreateFile(p_fileName);
  158. using (StreamWriter sw = new StreamWriter(strFileFullName, false, System.Text.Encoding.UTF8))
  159. {
  160. foreach (string strSql in p_sql)
  161. {
  162. sw.WriteLine(strSql);
  163. }
  164. sw.Close();
  165. }
  166. }
  167. }
  168. catch { }
  169. }
  170. /// <summary>
  171. /// 删除对应的文件
  172. /// </summary>
  173. /// <param name="p_fileName">文件名称</param>
  174. public static void DeleteLogFile(string p_fileName)
  175. {
  176. DeleteFile(p_fileName);
  177. }
  178. /// <summary>
  179. /// 根据文件名获取对应的文件内容,并返回文件内容
  180. /// </summary>
  181. /// <param name="p_fileName"></param>
  182. /// <returns></returns>
  183. public static byte[] ReadFile(string p_fileName)
  184. {
  185. string filePath = DataManager.LogFileStartPath + "\\Log\\";
  186. try
  187. {
  188. string strFileName = p_fileName;
  189. if (!p_fileName.Contains("."))
  190. strFileName = p_fileName + ".log";
  191. filePath = filePath + strFileName;
  192. if (File.Exists(filePath))
  193. {
  194. FileStream fsMyfile = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  195. byte[] byteFile = new byte[fsMyfile.Length];
  196. BinaryReader brMyfile = new BinaryReader(fsMyfile, Encoding.UTF8);
  197. brMyfile.Read(byteFile, 0, byteFile.Length);
  198. brMyfile.Close();
  199. fsMyfile.Close();
  200. fsMyfile.Dispose();
  201. return byteFile;
  202. }
  203. }
  204. catch (Exception ex)
  205. {
  206. throw ex;
  207. }
  208. return System.Text.Encoding.UTF8.GetBytes(filePath + " 日志文件不存在!");
  209. }
  210. /// <summary>
  211. /// 获取文件长度
  212. /// </summary>
  213. /// <param name="p_fileName"></param>
  214. /// <returns></returns>
  215. public static long GetFileSize(string p_fileName)
  216. {
  217. string filePath = DataManager.LogFileStartPath + "\\Log\\";
  218. try
  219. {
  220. string strFileName = p_fileName;
  221. if (!p_fileName.Contains("."))
  222. strFileName = p_fileName + ".log";
  223. filePath = filePath + strFileName;
  224. if (File.Exists(filePath))
  225. {
  226. FileStream fsMyfile = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  227. long fileSize = fsMyfile.Length;
  228. fsMyfile.Close();
  229. fsMyfile.Dispose();
  230. return fileSize;
  231. }
  232. }
  233. catch (Exception ex)
  234. {
  235. throw ex;
  236. }
  237. return 0;
  238. }
  239. #endregion
  240. #region 自定义方法
  241. /// <summary>
  242. /// 设置当前时间
  243. /// </summary>
  244. /// <returns>返回系统时间</returns>
  245. private static string GetNowDateTime()
  246. {
  247. DateTime dt = DateTime.Now;
  248. return dt.ToString("yyyy-MM-dd HH:mm:ss,fff");
  249. }
  250. /// <summary>
  251. /// 返回对应的文件全名
  252. /// </summary>
  253. /// <param name="p_fileName"></param>
  254. /// <returns>返回对应的文件全名</returns>
  255. private static string GetCreateFile(string p_fileName)
  256. {
  257. string filePath = DataManager.LogFileStartPath + "\\Log\\";
  258. DirectoryInfo dirFile = new DirectoryInfo(filePath);
  259. if (!dirFile.Exists)
  260. {
  261. dirFile.Create();
  262. }
  263. string strFileName = p_fileName;
  264. if (!p_fileName.Contains("."))
  265. {
  266. strFileName = p_fileName + ".log";
  267. }
  268. //if (strFileName.Contains(ConfigConst.LogFileNameOld))
  269. //{
  270. // string strTempFileAllName = filePath + strFileName;
  271. // if (System.IO.File.Exists(strTempFileAllName))
  272. // {
  273. // System.IO.FileInfo fileSize = new System.IO.FileInfo(strTempFileAllName);
  274. // if (fileSize.Length > (20 * 1024 * 1024)) //大于20M
  275. // {
  276. // strFileName = ConfigConst.LogFileNameOld + DateTime.Now.ToString("yyyyMMddHHmmss");
  277. // ConfigConst.LogFileName = strFileName;
  278. // strFileName = strFileName + ".log";
  279. // }
  280. // }
  281. //}
  282. return filePath + strFileName;
  283. }
  284. /// <summary>
  285. /// 删除日志文件信息
  286. /// </summary>
  287. /// <param name="p_fileName"></param>
  288. private static void DeleteFile(string p_fileName)
  289. {
  290. try
  291. {
  292. string filePath = DataManager.LogFileStartPath + "\\Log\\";
  293. string strFileName = p_fileName;
  294. if (!p_fileName.Contains("."))
  295. strFileName = p_fileName + ".log";
  296. filePath = filePath + strFileName;
  297. if (File.Exists(filePath))
  298. System.IO.File.Delete(filePath);
  299. }
  300. catch { }
  301. }
  302. #endregion
  303. }
  304. }