OutputLog.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*******************************************************************************
  2. * Copyright(c) 2012 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:OutputLog.cs
  5. * 2.功能描述:系统客户端错误日志输出类
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 欧阳涛 2012/06/07 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.IO;
  12. namespace Dongke.IBOSS.PRD.Basics.Library
  13. {
  14. #region 日志类型枚举
  15. /// <summary>
  16. /// 输出本地日志的类型
  17. /// </summary>
  18. public enum LogPriority
  19. {
  20. /// <summary>
  21. /// Debug测试
  22. /// </summary>
  23. Debug,
  24. /// <summary>
  25. /// 消息
  26. /// </summary>
  27. Information,
  28. /// <summary>
  29. /// 警告
  30. /// </summary>
  31. Warning,
  32. /// <summary>
  33. /// 错误
  34. /// </summary>
  35. Error,
  36. }
  37. #endregion
  38. public class OutputLog
  39. {
  40. #region 变量定义
  41. private const int DELETE_FILE_PERIOD = -30; // 日志文件删除周期,单位:天
  42. #endregion
  43. #region 公开方法/属性
  44. /// <summary>
  45. /// 日志文件写入方法
  46. /// </summary>
  47. /// <param name="priority">日志级别</param>
  48. /// <param name="fileName">出现错误文件名</param>
  49. /// <param name="methodName">出现错误方法</param>
  50. /// <param name="message">错误内容</param>
  51. public static void Trace(LogPriority priority, string fileName,
  52. string methodName, string message)
  53. {
  54. // 日志文件的路径
  55. string logFilePath = LocalPath.LogRootPath;
  56. // 消息写入日志文件
  57. TraceLog(priority, fileName, methodName, message, logFilePath);
  58. }
  59. #endregion
  60. #region 私有方法/函数
  61. /// <summary>
  62. /// 写日志文件方法
  63. /// </summary>
  64. /// <param name="priority">日志级别</param>
  65. /// <param name="bytes">字节</param>
  66. /// <param name="logFilePath">日志路径</param>
  67. /// <param name="logFileName">日志文件名</param>
  68. private static void TraceLog(LogPriority priority, byte[] bytes, string logFilePath, string logFileName)
  69. {
  70. try
  71. {
  72. // 日志文件存放的路径不存在,创建路径
  73. if (!Directory.Exists(logFilePath))
  74. {
  75. Directory.CreateDirectory(logFilePath);
  76. }
  77. // 日志文件名
  78. string logFile = logFilePath + logFileName;
  79. // 写入日志文本
  80. File.WriteAllBytes(logFile, bytes);
  81. }
  82. catch (Exception ex)
  83. {
  84. throw ex;
  85. }
  86. }
  87. /// <summary>
  88. /// 书写错误日志
  89. /// </summary>
  90. /// <param name="priority">日志级别</param>
  91. /// <param name="fileName">错误的源文件名</param>
  92. /// <param name="methodName">错误的源方法</param>
  93. /// <param name="message">错误消息</param>
  94. /// <param name="logFilePath">错误日志路径</param>
  95. public static void TraceLog(LogPriority priority, string fileName,
  96. string methodName, string message, string logFilePath)
  97. {
  98. try
  99. {
  100. // 日志文件存放的路径不存在,创建路径
  101. if (!Directory.Exists(logFilePath))
  102. {
  103. Directory.CreateDirectory(logFilePath);
  104. }
  105. // 文件名
  106. string logFile =
  107. logFilePath + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
  108. // 文件名不存在的情况下,删除旧文件
  109. if (!File.Exists(logFile))
  110. {
  111. // 旧文件删除
  112. DeleteLogFile(logFilePath);
  113. }
  114. // 把错误消息写入日志文件
  115. System.Text.StringBuilder writeMessage = new System.Text.StringBuilder();
  116. //writeMessage.AppendLine();
  117. writeMessage.AppendLine("---------------------------------------------------------------------------");
  118. writeMessage.Append(GetLogPriority(priority).PadRight(14, ' '));
  119. writeMessage.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").PadRight(14, ' '));
  120. writeMessage.Append(fileName.PadRight(20, ' ') + " ");
  121. writeMessage.Append(methodName.PadRight(20, ' ') + " ");
  122. writeMessage.AppendLine();
  123. writeMessage.Append(message);
  124. writeMessage.AppendLine();
  125. writeMessage.AppendLine("---------------------------------------------------------------------------");
  126. // 日志文件写入
  127. WriteLogFile(priority, writeMessage.ToString(), logFile);
  128. }
  129. catch (Exception ex)
  130. {
  131. throw ex;
  132. }
  133. }
  134. /// <summary>
  135. /// 写日志方法
  136. /// </summary>
  137. /// <param name="priority">日志级别</param>
  138. /// <param name="message">日志消息</param>
  139. /// <param name="fileName">写入文件名</param>
  140. private static void WriteLogFile(LogPriority priority, string message, string fileName)
  141. {
  142. try
  143. {
  144. StreamWriter swStreamWriter = File.AppendText(fileName);
  145. swStreamWriter.WriteLine(message);
  146. swStreamWriter.Close();
  147. }
  148. catch (Exception ex)
  149. {
  150. throw ex;
  151. }
  152. }
  153. /// <summary>
  154. /// 删除非法日志文件和过时的日志文件
  155. /// </summary>
  156. /// <param name="logFilePath">日志文件路径</param>
  157. private static void DeleteLogFile(string logFilePath)
  158. {
  159. try
  160. {
  161. // 日志文件夹里面的所有的日志文件的取得
  162. //DirectoryInfo diDirectoryInfo = new DirectoryInfo(logFilePath);
  163. //FileInfo[] fiFileInfo = diDirectoryInfo.GetFiles();
  164. //// 旧文件删除
  165. //string strLogFile = string.Empty;
  166. //foreach (FileInfo fileInfo in fiFileInfo)
  167. //{
  168. // strLogFile = fileInfo.Name.Remove(fileInfo.Name.LastIndexOf('.'));
  169. // DateTime? datDateTime;
  170. // bool bolCheckValid = Utility.IsValidDate(strLogFile, out datDateTime);
  171. // if (!bolCheckValid)
  172. // {
  173. // // 无效的文件删除
  174. // File.Delete(fileInfo.FullName);
  175. // }
  176. // else
  177. // {
  178. // // 旧文件的删除
  179. // if (datDateTime < DateTime.Now.AddDays(DELETE_FILE_PERIOD))
  180. // {
  181. // File.Delete(fileInfo.FullName);
  182. // }
  183. // }
  184. //}
  185. string[] files = System.IO.Directory.GetFiles(logFilePath, "*.log");
  186. if (files != null && files.Length > 0)
  187. {
  188. DateTime deathDate = DateTime.Now.Date.AddDays(-60);
  189. foreach (string file in files)
  190. {
  191. try
  192. {
  193. //if (File.GetCreationTime(file) < deathDate)
  194. if (File.GetLastWriteTime(file) < deathDate)
  195. {
  196. File.Delete(file);
  197. }
  198. }
  199. catch
  200. {
  201. }
  202. }
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. //throw ex;
  208. }
  209. }
  210. /// <summary>
  211. /// 取得日志的级别
  212. /// </summary>
  213. /// <param name="priority">日志级别枚举</param>
  214. /// <returns></returns>
  215. private static string GetLogPriority(LogPriority priority)
  216. {
  217. try
  218. {
  219. string strLogPriority = string.Empty;
  220. switch (priority)
  221. {
  222. case LogPriority.Debug:
  223. strLogPriority = "[DEBUG]";
  224. break;
  225. case LogPriority.Information:
  226. strLogPriority = "[INFORMATION]";
  227. break;
  228. case LogPriority.Error:
  229. strLogPriority = "[ERROR]";
  230. break;
  231. case LogPriority.Warning:
  232. strLogPriority = "[WARNING]";
  233. break;
  234. default:
  235. break;
  236. }
  237. return strLogPriority;
  238. }
  239. catch (Exception ex)
  240. {
  241. throw ex;
  242. }
  243. }
  244. #endregion
  245. }
  246. }