TxtLog.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. namespace MvvmScaffoldFrame48.DLL.LogTools
  6. {
  7. public class TxtLog
  8. {
  9. #region 变量
  10. static public int min = 0;
  11. static public int max = 10;
  12. #endregion
  13. #region 实例
  14. static public StringBuilder _log = new StringBuilder();
  15. static TxtGenerate fio;
  16. #endregion
  17. #region 构造方法
  18. static TxtLog()
  19. {
  20. fio = new TxtGenerate();
  21. fio.OpenWriteFile("log.txt");
  22. }
  23. #endregion
  24. #region 静态方法
  25. static public void error(object str)
  26. {
  27. log("Erorr:" + str, 10);
  28. }
  29. /// <summary>
  30. /// log写入
  31. /// </summary>
  32. /// <param name="str"></param>
  33. static public void log(object str)
  34. {
  35. log(str, 0);
  36. }
  37. static public void log(string str)
  38. {
  39. log(str, 0);
  40. }
  41. /// <summary>
  42. /// log等级定制
  43. /// </summary>
  44. /// <param name="p"></param>
  45. /// <param name="level"></param>
  46. public static void log(object p, int level)
  47. {
  48. log(p.ToString(), level);
  49. }
  50. static public void log(string str, int level)
  51. {
  52. if (level < fio.LogLevel)
  53. {
  54. return;
  55. }
  56. if (str == null)
  57. {
  58. str = "null";
  59. }
  60. fio.WriteLine(DateTime.Now.ToString("O") + "->" + str + "\r\n");
  61. }
  62. /// <summary>
  63. /// 修改Log的限制等级0日志最多,10最少
  64. /// </summary>
  65. /// <param name="Loglevel"></param>
  66. public static void ChangeLogLevel(int Loglevel)
  67. {
  68. fio.LogLevel = Loglevel;
  69. }
  70. public static void LogSurPlusMemory()
  71. {
  72. PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");
  73. var ramAvailable = ramCounter.NextValue();
  74. string ramAvaiableStr = string.Format("{0} MB", ramAvailable);
  75. log($"剩余物理内存:{ramAvaiableStr}");
  76. }
  77. #endregion
  78. }
  79. public class TxtGenerate
  80. {
  81. #region 变量
  82. private string LogFloderPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Log\\";
  83. private double LogFileSize = 128 * Math.Pow(1024, 3);
  84. public int LogLevel = 5;
  85. #endregion
  86. #region 实例
  87. private FileStream fsr;
  88. private FileStream fsw;
  89. private StreamWriter sw;
  90. private StreamReader sr;
  91. #endregion
  92. #region 共有方法
  93. /// <summary>
  94. /// 创建用于读取文件行的文件流和StreamWriter对象
  95. /// </summary>
  96. /// <param name="file"></param>
  97. public void OpenReadFile(string file)
  98. {
  99. if (!File.Exists(file))
  100. File.Create(file).Close();
  101. fsr = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
  102. sr = new StreamReader(fsr);
  103. }
  104. /// <summary>
  105. /// 关闭读文件流
  106. /// </summary>
  107. public void CloseReadFile()
  108. {
  109. if (fsr != null)
  110. fsr.Close();
  111. }
  112. /// <summary>
  113. /// 创建用于向文件中追加行的文件流和StreamWriter对象
  114. /// </summary>
  115. /// <param name="file"></param>
  116. public void OpenWriteFile(string file)
  117. {
  118. int i = 1;
  119. while (true)
  120. {
  121. if (!Directory.Exists(LogFloderPath))//如果不存在就创建file文件夹
  122. {
  123. Directory.CreateDirectory(LogFloderPath);
  124. }
  125. if (!File.Exists(LogFloderPath + file)) // 如果文件不存在,先创建这个文件
  126. File.Create(LogFloderPath + file).Close();
  127. // 以追加模式打开这个文件
  128. fsw = new FileStream(LogFloderPath + file, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  129. if (fsw.Length > LogFileSize)
  130. {
  131. file = "log" + i + ".txt";
  132. i++;
  133. continue;
  134. }
  135. break;
  136. }
  137. // 根据创建的FileStream对象来创建StreamWriter对象
  138. sw = new StreamWriter(fsw);
  139. }
  140. /// <summary>
  141. /// 关闭写文件流
  142. /// </summary>
  143. public void CloseWriteFile()
  144. {
  145. if (fsw != null)
  146. fsw.Close();
  147. }
  148. /// <summary>
  149. /// 从文件中读取一行
  150. /// </summary>
  151. /// <returns></returns>
  152. public string ReadLine()
  153. {
  154. if (sr.EndOfStream) // 如果文件流指针已经指向文件尾部,返回null
  155. return null;
  156. return sr.ReadLine();
  157. }
  158. /// <summary>
  159. /// 向文件中追加一行字符串
  160. /// </summary>
  161. /// <param name="s"></param>
  162. public void WriteLine(string s)
  163. {
  164. if (fsw.Length > LogFileSize)
  165. {
  166. OpenWriteFile("log.txt");
  167. }
  168. lock (sw)
  169. {
  170. sw.WriteLine(s);
  171. sw.Flush(); // 刷新写入缓冲区,使这一行对于读文件流可见
  172. }
  173. }
  174. /// <summary>
  175. /// 用于判断文件流指针是否位于文件尾部
  176. /// </summary>
  177. /// <returns></returns>
  178. public bool IsEof()
  179. {
  180. return sr.EndOfStream;
  181. }
  182. #endregion
  183. }
  184. }