LogOut.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Curtain.Framework.Windows;
  5. namespace PCLCommunication
  6. {
  7. /// <summary>
  8. /// 日志输出
  9. /// </summary>
  10. public static class LogOut
  11. {
  12. public static string LogDirectory = ApplicationInformation.StartupDirectory + "Log\\";
  13. public static string Prefix_Name = null;
  14. public static int File_Exist_Days = 30;
  15. public static int File_Max_Size = 10;// 10m
  16. private static object lock_object = new object();
  17. /// <summary>
  18. /// 异常日志
  19. /// </summary>
  20. /// <param name="moduleName"></param>
  21. /// <param name="ex"></param>
  22. /// <param name="message"></param>
  23. public static void Error(string moduleName, Exception ex, string message = null)
  24. {
  25. if (ex == null && message == null)
  26. {
  27. return;
  28. }
  29. lock (lock_object)
  30. {
  31. string pp = LogDirectory + "Error\\";
  32. if (!string.IsNullOrWhiteSpace(moduleName))
  33. {
  34. pp += moduleName + "\\";
  35. }
  36. if (!Directory.Exists(pp))
  37. {
  38. Directory.CreateDirectory(pp);
  39. }
  40. string path = pp + "error_" + Prefix_Name + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
  41. DeleteFiles(pp);
  42. BackFile(path);
  43. StringBuilder db = new StringBuilder("-------------------------------------------------------------------------");
  44. db.AppendLine();
  45. db.AppendLine("[ERROR] " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
  46. System.Diagnostics.StackTrace ss = new System.Diagnostics.StackTrace(true);
  47. System.Reflection.MethodBase mb = ss.GetFrame(1).GetMethod();
  48. ////取得父方法命名空间
  49. //db.AppendLine(mb.DeclaringType.Namespace);
  50. ////取得父方法类名
  51. //db.AppendLine(mb.DeclaringType.Name);
  52. ////取得父方法类全名
  53. //db.AppendLine(mb.DeclaringType.FullName);
  54. ////取得父方法名
  55. //db.AppendLine(mb.Name);
  56. db.AppendLine(mb.DeclaringType.ToString() + "[" + mb.ToString() + "]");
  57. if (message != null)
  58. {
  59. db.Append("Message : ");
  60. db.AppendLine(message);
  61. }
  62. if (ex != null)
  63. {
  64. db.AppendLine("Exception : ");
  65. db.AppendLine(ex.Message);
  66. }
  67. File.AppendAllText(path, db.ToString());
  68. }
  69. }
  70. /// <summary>
  71. /// 调试日志
  72. /// </summary>
  73. /// <param name="moduleName"></param>
  74. /// <param name="ex"></param>
  75. /// <param name="message"></param>
  76. public static void Debug(string moduleName, string message)
  77. {
  78. if (message == null)
  79. {
  80. return;
  81. }
  82. lock (lock_object)
  83. {
  84. string pp = LogDirectory + "Debug\\";
  85. if (!string.IsNullOrWhiteSpace(moduleName))
  86. {
  87. pp += moduleName + "\\";
  88. }
  89. if (!Directory.Exists(pp))
  90. {
  91. Directory.CreateDirectory(pp);
  92. }
  93. string path = pp + "debug_" + Prefix_Name + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
  94. DeleteFiles(pp);
  95. BackFile(path);
  96. StringBuilder db = new StringBuilder("-------------------------------------------------------------------------");
  97. db.AppendLine();
  98. db.AppendLine("[DEBUG] " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
  99. System.Diagnostics.StackTrace ss = new System.Diagnostics.StackTrace(true);
  100. System.Reflection.MethodBase mb = ss.GetFrame(1).GetMethod();
  101. ////取得父方法命名空间
  102. //db.AppendLine(mb.DeclaringType.Namespace);
  103. ////取得父方法类名
  104. //db.AppendLine(mb.DeclaringType.Name);
  105. ////取得父方法类全名
  106. //db.AppendLine(mb.DeclaringType.FullName);
  107. ////取得父方法名
  108. //db.AppendLine(mb.Name);
  109. db.AppendLine(mb.DeclaringType.ToString() + "[" + mb.ToString() + "]");
  110. if (message != null)
  111. {
  112. db.Append("Message : ");
  113. db.AppendLine(message);
  114. }
  115. File.AppendAllText(path, db.ToString());
  116. }
  117. }
  118. /// <summary>
  119. /// 调试日志
  120. /// </summary>
  121. /// <param name="moduleName"></param>
  122. /// <param name="ex"></param>
  123. /// <param name="message"></param>
  124. public static void Info(string moduleName, string message)
  125. {
  126. if (message == null)
  127. {
  128. return;
  129. }
  130. lock (lock_object)
  131. {
  132. string pp = LogDirectory + "Info\\";
  133. if (!string.IsNullOrWhiteSpace(moduleName))
  134. {
  135. pp += moduleName + "\\";
  136. }
  137. if (!Directory.Exists(pp))
  138. {
  139. Directory.CreateDirectory(pp);
  140. }
  141. string path = pp + "info_" + Prefix_Name + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
  142. DeleteFiles(pp);
  143. BackFile(path);
  144. StringBuilder db = new StringBuilder("-------------------------------------------------------------------------");
  145. db.AppendLine();
  146. db.AppendLine("[INFO] " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
  147. System.Diagnostics.StackTrace ss = new System.Diagnostics.StackTrace(true);
  148. System.Reflection.MethodBase mb = ss.GetFrame(1).GetMethod();
  149. ////取得父方法命名空间
  150. //db.AppendLine(mb.DeclaringType.Namespace);
  151. ////取得父方法类名
  152. //db.AppendLine(mb.DeclaringType.Name);
  153. ////取得父方法类全名
  154. //db.AppendLine(mb.DeclaringType.FullName);
  155. ////取得父方法名
  156. //db.AppendLine(mb.Name);
  157. db.AppendLine(mb.DeclaringType.ToString() + "[" + mb.ToString() + "]");
  158. if (message != null)
  159. {
  160. db.Append("Message : ");
  161. db.AppendLine(message);
  162. }
  163. File.AppendAllText(path, db.ToString());
  164. }
  165. }
  166. private static void BackFile(string fName)
  167. {
  168. try
  169. {
  170. if (File.Exists(fName))
  171. {
  172. FileInfo fi = new FileInfo(fName);
  173. double l = (double)fi.Length / 1024 / 1024;
  174. if (l > File_Max_Size)
  175. {
  176. fName = fName.Remove(fName.Length - 4) + "_" + DateTime.Now.ToString("HHmmss") + ".log";
  177. fi.MoveTo(fName);
  178. }
  179. }
  180. }
  181. catch
  182. {
  183. }
  184. }
  185. private static void DeleteFiles(string path)
  186. {
  187. try
  188. {
  189. string[] dirs = Directory.GetFiles(path);
  190. if (dirs != null && dirs.Length > 0)
  191. {
  192. DateTime dateNow = DateTime.Now.Date.AddDays(0 - File_Exist_Days);
  193. foreach (string item in dirs)
  194. {
  195. if (File.GetCreationTime(item) < dateNow)
  196. {
  197. File.Delete(item);
  198. }
  199. }
  200. }
  201. }
  202. catch
  203. {
  204. }
  205. }
  206. }
  207. }