平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LogHelper.cs 8.5 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. namespace SafeCampus.Core.Extension
  2. {
  3. /// <summary>
  4. /// 日志帮助类
  5. /// </summary>
  6. public class LogHelper
  7. {
  8. /// <summary>
  9. /// 配置文件
  10. /// </summary>
  11. protected static LogConfig Config = new LogConfig();
  12. /// <summary>
  13. /// 写日志
  14. /// </summary>
  15. /// <param name="isError"></param>
  16. /// <returns></returns>
  17. protected static StreamWriterLock GetLogWriter(bool isError = false)
  18. {
  19. try
  20. {
  21. string root = AppDomain.CurrentDomain.BaseDirectory;
  22. DateTime now = DateTime.Now;
  23. //日志文件路径
  24. string folder = isError ? Config.LogErrorFolder : Config.LogInfoFolder;
  25. folder = Path.Combine(root, folder, now.ToString(Config.LogFolderFormat));
  26. string filePath = Path.Combine(folder, now.ToString(Config.LogFileFormat) + Config.LogFileExt);
  27. if (!Directory.Exists(folder))
  28. {
  29. Directory.CreateDirectory(folder);
  30. }
  31. return new StreamWriterLock(filePath);
  32. }
  33. catch (Exception)
  34. {
  35. return null;
  36. }
  37. }
  38. /// <summary>
  39. /// 写日志
  40. /// </summary>
  41. public static void WriteToLog(string content)
  42. {
  43. using (var sw = GetLogWriter())
  44. {
  45. var writer = sw.Writer;
  46. writer.WriteStart();
  47. writer.WriteTime();
  48. writer.WriteContent(content);
  49. writer.WriteLine();
  50. }
  51. }
  52. /// <summary>
  53. /// 写日志
  54. /// </summary>
  55. public static void WriteToLog(string[] contents)
  56. {
  57. using (var sw = GetLogWriter())
  58. {
  59. var writer = sw.Writer;
  60. writer.WriteStart();
  61. writer.WriteTime();
  62. writer.WriteContent(contents);
  63. writer.WriteEnd();
  64. }
  65. }
  66. /// <summary>
  67. /// 写日志
  68. /// </summary>
  69. public static void WriteToLog(string title, string content)
  70. {
  71. using (var sw = GetLogWriter())
  72. {
  73. var writer = sw.Writer;
  74. writer.WriteStart();
  75. writer.WriteTitle(title);
  76. writer.WriteTime();
  77. writer.WriteContent(content);
  78. writer.WriteEnd();
  79. }
  80. }
  81. /// <summary>
  82. /// 写日志(错误)
  83. /// </summary>
  84. public static void WriteToLog(Exception ex, string title = "", string content = "")
  85. {
  86. using (var sw = GetLogWriter(true))
  87. {
  88. var writer = sw.Writer;
  89. writer.WriteStart();
  90. writer.WriteTitle(title, true);
  91. writer.WriteTime();
  92. if (!string.IsNullOrEmpty(content))
  93. {
  94. writer.WriteContent(content);
  95. }
  96. writer.WriteException(ex);
  97. writer.WriteEnd();
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// 日志目录配置
  103. /// </summary>
  104. public class LogConfig
  105. {
  106. /// <summary>
  107. /// 构造函数
  108. /// </summary>
  109. public LogConfig()
  110. {
  111. this.LogRoot = "Log";
  112. this.LogInfoFolder = Path.Combine(this.LogRoot, "LogInfo");
  113. this.LogErrorFolder = Path.Combine(this.LogRoot, "LogError");
  114. this.LogFolderFormat = "yyyyMMdd";
  115. this.LogFileFormat = "yyyyMMdd_HH";
  116. this.LogFileExt = ".log";
  117. }
  118. /// <summary>
  119. /// 日志根目录
  120. /// </summary>
  121. public string LogRoot { get; set; }
  122. /// <summary>
  123. /// 普通信息
  124. /// </summary>
  125. public string LogInfoFolder { get; set; }
  126. /// <summary>
  127. /// 错误信息
  128. /// </summary>
  129. public string LogErrorFolder { get; set; }
  130. /// <summary>
  131. /// 文件夹日期格式
  132. /// </summary>
  133. public string LogFolderFormat { get; set; }
  134. /// <summary>
  135. /// 文件日志格式
  136. /// </summary>
  137. public string LogFileFormat { get; set; }
  138. /// <summary>
  139. /// 文件后缀名
  140. /// </summary>
  141. public string LogFileExt { get; set; }
  142. }
  143. /// <summary>
  144. /// 日志工具
  145. /// </summary>
  146. public static class LogTools
  147. {
  148. /// <summary>
  149. /// 开始写日志
  150. /// </summary>
  151. /// <param name="writer"></param>
  152. public static void WriteStart(this StreamWriter writer)
  153. {
  154. writer.WriteLine("-------------------------------------------------------------------------------------");
  155. }
  156. /// <summary>
  157. /// 结束写
  158. /// </summary>
  159. /// <param name="writer"></param>
  160. public static void WriteEnd(this StreamWriter writer)
  161. {
  162. //writer.WriteLine("-------------------------------------------------------------------------------------");
  163. writer.WriteLine();
  164. }
  165. /// <summary>
  166. /// 写入时间
  167. /// </summary>
  168. /// <param name="writer"></param>
  169. public static void WriteTime(this StreamWriter writer)
  170. {
  171. writer.WriteLine("Date:{0:yyyy-MM-dd} Time:{0:HH:mm:ss}", DateTime.Now);
  172. }
  173. /// <summary>
  174. /// 写入标题
  175. /// </summary>
  176. /// <param name="writer"></param>
  177. /// <param name="title"></param>
  178. /// <param name="isError"></param>
  179. public static void WriteTitle(this StreamWriter writer, string title, bool isError = false)
  180. {
  181. if (isError)
  182. {
  183. writer.WriteLine("Title:Error!!!");
  184. if (!string.IsNullOrEmpty(title))
  185. {
  186. writer.WriteLine(title);
  187. }
  188. }
  189. else
  190. {
  191. writer.WriteLine("Title:{0}", title);
  192. }
  193. }
  194. /// <summary>
  195. /// 写入内容
  196. /// </summary>
  197. /// <param name="writer"></param>
  198. /// <param name="content"></param>
  199. public static void WriteContent(this StreamWriter writer, string content)
  200. {
  201. writer.WriteLine("Content:{0}", content);
  202. }
  203. /// <summary>
  204. /// 写入内容
  205. /// </summary>
  206. /// <param name="writer"></param>
  207. /// <param name="contents"></param>
  208. public static void WriteContent(this StreamWriter writer, string[] contents)
  209. {
  210. writer.WriteLine("Content:{0}", contents.Length);
  211. var dig = Math.Max(2, contents.Length.ToString().Length); //位数
  212. for (int i = 0; i < contents.Length; i++)
  213. {
  214. writer.WriteLine("No.{0}->{1}", (i + 1).ToString().PadLeft(2, '0'), contents[i]);
  215. }
  216. }
  217. /// <summary>
  218. /// 写入异常
  219. /// </summary>
  220. /// <param name="writer"></param>
  221. /// <param name="ex"></param>
  222. public static void WriteException(this StreamWriter writer, Exception ex)
  223. {
  224. writer.WriteLine("Message:{0}", ex.Message);
  225. writer.WriteLine("StackTrace:{0}", ex.StackTrace);
  226. if (ex.InnerException != null)
  227. {
  228. writer.WriteLine("InnerException:");
  229. writer.WriteLine("Message:{0}", ex.InnerException.Message);
  230. writer.WriteLine("StackTrace:{0}", ex.InnerException.StackTrace);
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// 文件锁
  236. /// </summary>
  237. public class StreamWriterLock : IDisposable
  238. {
  239. /// <summary>
  240. /// 写入
  241. /// </summary>
  242. public StreamWriter Writer { get; protected set; }
  243. /// <summary>
  244. /// 构造函数
  245. /// </summary>
  246. /// <param name="filePath"></param>
  247. public StreamWriterLock(string filePath)
  248. {
  249. Lock.EnterWriteLock();
  250. if (!File.Exists(filePath))//如果文件不存在
  251. {
  252. File.Create(filePath).Close();
  253. }
  254. this.Writer = File.AppendText(filePath);
  255. }
  256. /// <summary>
  257. /// 文件读写锁
  258. /// </summary>
  259. protected static ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
  260. public void Dispose()
  261. {
  262. this.Writer.Dispose();
  263. Lock.ExitWriteLock();
  264. }
  265. }
  266. }