LogHelper.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Common
  7. {
  8. public class LogHelper
  9. {
  10. public static string LogInfo = AppDomain.CurrentDomain.BaseDirectory + "Logs\\";
  11. /// <summary>
  12. /// 获取日志存储路径
  13. /// </summary>
  14. /// <param name="logfilename"></param>
  15. /// <param name="type">0正常信息,1错误信息</param>
  16. /// <returns></returns>
  17. private static string GetLogFilePath(string logfilename, int type = 0)
  18. {
  19. if (!System.IO.Directory.Exists(LogInfo))
  20. {
  21. System.IO.Directory.CreateDirectory(LogInfo);
  22. }
  23. string log = LogInfo + DateTime.Now.ToString("yyyy-MM-dd") + logfilename + ".txt";
  24. if (type != 0)
  25. {
  26. log = LogInfo + logfilename + ".txt";
  27. }
  28. return log;
  29. }
  30. /// <summary>
  31. /// 保存的最多日志数量
  32. /// </summary>
  33. // private static int MaxLogCount = 30;
  34. /// <summary>
  35. /// 日志信息
  36. /// </summary>
  37. /// <param name="msg"></param>
  38. /// <param name="logfilename"></param>
  39. public static void Info(string msg, string logfilename = "",int type = 0)
  40. {
  41. try
  42. {
  43. DeleteLog();
  44. string datapath = GetLogFilePath(logfilename, type);
  45. if (type != 0)
  46. {
  47. using (FileStream fs = new FileStream(datapath, FileMode.Create))
  48. {
  49. using (StreamWriter sw = new StreamWriter(fs))
  50. {
  51. //开始写入
  52. sw.WriteLine("当前时间:" + DateTime.Now.ToString());
  53. sw.WriteLine("日志级别:Error");
  54. sw.WriteLine("偏移量:"+ logfilename);
  55. sw.WriteLine("日志信息:" + msg);
  56. //清空缓冲区
  57. sw.Flush();
  58. //关闭流
  59. sw.Close();
  60. fs.Close();
  61. }
  62. }
  63. return;
  64. }
  65. if (System.IO.File.Exists(datapath))//追加
  66. {
  67. using (FileStream fs = new FileStream(datapath, FileMode.Append))
  68. {
  69. using (StreamWriter sw = new StreamWriter(fs))
  70. {
  71. //开始写入
  72. sw.WriteLine("\n");
  73. sw.WriteLine("当前时间:" + DateTime.Now.ToString());
  74. sw.WriteLine("日志级别:Info");
  75. sw.WriteLine("日志信息:" + msg);
  76. //清空缓冲区
  77. sw.Flush();
  78. //关闭流
  79. sw.Close();
  80. fs.Close();
  81. }
  82. }
  83. }
  84. else//创建
  85. {
  86. using (FileStream fs = new FileStream(datapath, FileMode.Create))
  87. {
  88. using (StreamWriter sw = new StreamWriter(fs))
  89. {
  90. //开始写入
  91. sw.WriteLine("当前时间:" + DateTime.Now.ToString());
  92. sw.WriteLine("日志级别:Info");
  93. sw.WriteLine("日志信息:" + msg);
  94. //清空缓冲区
  95. sw.Flush();
  96. //关闭流
  97. sw.Close();
  98. fs.Close();
  99. }
  100. }
  101. }
  102. }
  103. catch
  104. {
  105. }
  106. }
  107. /// <summary>
  108. /// 错误日志
  109. /// </summary>
  110. /// <param name="ex"></param>
  111. /// <param name="logfilename"></param>
  112. public static void Error(Exception ex, string logfilename = "", int type = 0)
  113. {
  114. try
  115. {
  116. DeleteLog();
  117. string datapath = GetLogFilePath(logfilename, type);
  118. if (System.IO.File.Exists(datapath))//追加
  119. {
  120. using (FileStream fs = new FileStream(datapath, FileMode.Append))
  121. {
  122. using (StreamWriter sw = new StreamWriter(fs))
  123. { //开始写入
  124. sw.WriteLine("\n");
  125. sw.WriteLine("当前时间:" + DateTime.Now.ToString());
  126. sw.WriteLine("异常级别:Error");
  127. sw.WriteLine("异常信息:" + ex.Message);
  128. sw.WriteLine("异常对象:" + ex.Source);
  129. sw.WriteLine("异常位置:" + ex.StackTrace);
  130. //清空缓冲区
  131. sw.Flush();
  132. //关闭流
  133. sw.Close();
  134. fs.Close();
  135. }
  136. }
  137. }
  138. else//创建
  139. {
  140. using (FileStream fs = new FileStream(datapath, FileMode.Create))
  141. {
  142. using (StreamWriter sw = new StreamWriter(fs))
  143. {
  144. //开始写入
  145. sw.WriteLine("当前时间:" + DateTime.Now.ToString());
  146. sw.WriteLine("异常级别:Error");
  147. sw.WriteLine("异常信息:" + ex.Message);
  148. sw.WriteLine("异常对象:" + ex.Source);
  149. sw.WriteLine("异常位置:" + ex.StackTrace);
  150. //清空缓冲区
  151. sw.Flush();
  152. //关闭流
  153. sw.Close();
  154. fs.Close();
  155. }
  156. }
  157. }
  158. }
  159. catch
  160. {
  161. }
  162. }
  163. /// <summary>
  164. /// 删除多余日志
  165. /// </summary>
  166. private static void DeleteLog()
  167. {
  168. //不必每天都删除日志,整10天删除
  169. if (!(DateTime.Now.Day % 10 == 0))
  170. {
  171. return;
  172. }
  173. DirectoryInfo dic = new DirectoryInfo(LogInfo);
  174. if (dic.Exists)
  175. {
  176. try
  177. {
  178. //清理10天前的日志文件
  179. dic.GetFiles("*.txt", SearchOption.AllDirectories).Where(n => (DateTime.Now - n.LastWriteTime).TotalDays > 10).ToList().TrueForAll(n => { n.Delete(); return true; });
  180. }
  181. catch (Exception ex)
  182. {
  183. Error(ex);
  184. }
  185. }
  186. }
  187. }
  188. }