Logs.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace SnapshotWinFormsApp.Tools
  7. {
  8. /// <summary>
  9. /// 最基本的日志记录
  10. /// </summary>
  11. public static class Logs
  12. {
  13. /// <summary>
  14. /// 获取记录文件路径
  15. /// </summary>
  16. /// <returns>日志文件路径</returns>
  17. public static string Path()
  18. {
  19. var logPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "logs");
  20. if (!Directory.Exists(logPath))
  21. {
  22. Directory.CreateDirectory(logPath);
  23. }
  24. return logPath;
  25. }
  26. /// <summary>
  27. /// 错误信息记录
  28. /// 请在所有的日志记录都无法使用的情况下使用下, 再使用本方法
  29. /// </summary>
  30. /// <param name="msg">记录的内容</param>
  31. public static void Err(string msg)
  32. {
  33. string logPath = System.IO.Path.Combine(Path(), "err.log");
  34. StackTrace trace = new StackTrace();
  35. MethodBase methodBase = trace.GetFrame(1).GetMethod();
  36. msg = $"方法 {methodBase.Name} 异常: {msg}";
  37. Note(logPath, msg, true);
  38. }
  39. /// <summary>
  40. /// 错误信息记录
  41. /// </summary>
  42. /// <param name="e"></param>
  43. public static void Err(Exception e)
  44. {
  45. var msg = string.Empty;
  46. try
  47. {
  48. msg = "Form: " + e?.InnerException?.Source + "\r\nMsg: " + e;
  49. }
  50. catch
  51. {
  52. msg = e.ToString();
  53. }
  54. Err(msg);
  55. }
  56. /// <summary>
  57. /// 记录警告信息
  58. /// </summary>
  59. /// <param name="msg">内容</param>
  60. public static void Warning(string msg)
  61. {
  62. Note(System.IO.Path.Combine(Path() , "Warning.log"),msg,true);
  63. }
  64. /// <summary>
  65. /// 错误信息记录
  66. /// </summary>
  67. /// <param name="msg">记录的内容</param>
  68. /// <param name="name">文件名字</param>
  69. public static void Err(string msg, string name)
  70. {
  71. string path = System.IO.Path.Combine(Path(), DateTime.Now.ToString("yyyyMMdd") + "[" + name + "][err].log");
  72. Note(path, msg, true);
  73. }
  74. /// <summary>
  75. /// 错误信息记录
  76. /// </summary>
  77. /// <param name="method">方法</param>
  78. /// <param name="param">参数</param>
  79. /// <param name="discription">描述</param>
  80. public static void Err(string method, string param ,string discription)
  81. {
  82. string message = "方法: " + method + "\r\n参数:" + param + "\r\n描述:" + discription;
  83. Err(message);
  84. }
  85. /// <summary>
  86. /// 日志记录
  87. /// </summary>
  88. /// <param name="msg">记录的内容</param>
  89. public static void Note(string msg)
  90. {
  91. var logPath = System.IO.Path.Combine(Path(), "note.log");
  92. Note(logPath, msg, true);
  93. }
  94. /// <summary>
  95. /// 日志记录
  96. /// 日志自动记录调用者的名字
  97. /// </summary>
  98. /// <param name="msg">记录的信息</param>
  99. public static void MethodNote(string msg)
  100. {
  101. StackTrace trace = new StackTrace();
  102. MethodBase metbodBase = trace.GetFrame(1).GetMethod();
  103. Note(System.IO.Path.Combine(Path(), "note.log"), metbodBase.Name + ":" + msg);
  104. }
  105. /// <summary>
  106. /// 日志记录
  107. /// </summary>
  108. /// <param name="path">日志文件路径</param>
  109. /// <param name="msg">内容</param>
  110. public static void Note(string path, string msg)
  111. {
  112. Note(path, msg, false);
  113. }
  114. /// <summary>
  115. /// 日志记录
  116. /// </summary>
  117. /// <param name="msg">记录的内容</param>
  118. /// <param name="path">日志文件路径</param>
  119. /// <param name="format">是否格式化信息</param>
  120. public static void Note(string path, string msg, bool format)
  121. {
  122. try
  123. {
  124. if (!File.Exists(path))
  125. {
  126. FileStream fs = File.Create(path);
  127. fs.Dispose();
  128. fs.Close();
  129. }
  130. using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Write))
  131. {
  132. StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
  133. if (format) sw.WriteLine(DateTime.Now + " " + msg);
  134. else sw.WriteLine(msg);
  135. sw.Dispose();
  136. sw.Close();
  137. }
  138. }
  139. catch
  140. {
  141. }
  142. }
  143. }
  144. }