地铁二期项目正式开始

SysLog.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace YTSoft.Common
  6. {
  7. /// <summary>
  8. /// 记录系统错误日志类
  9. /// </summary>
  10. public class SysLog
  11. {
  12. #region 错误记录
  13. private static Exception EX = null;//错误消息
  14. private static string Leve = null;//错误级别
  15. private static string Position = null;//错误位置
  16. private static string SavePath = null; //保存位置
  17. /// <summary>
  18. /// 向文件中写错误日志,只写错误信息
  19. /// </summary>
  20. /// <param name="ex">错误信息</param>
  21. public static void WriteLog(Exception ex)
  22. {
  23. EX = ex;
  24. WriteDisk();//文件写入硬盘
  25. }
  26. /// <summary>
  27. /// 向文件中写错误日志,只写错误信息
  28. /// </summary>
  29. /// <param name="ex">错误信息</param>
  30. public static void WriteLog(Exception ex, string path)
  31. {
  32. SavePath = path;
  33. EX = ex;
  34. WriteDisk();//文件写入硬盘
  35. }
  36. /// <summary>
  37. /// 向文件中写错误日志,记录错误信息,发生错误位置
  38. /// </summary>
  39. /// <param name="ex">错误信息</param>
  40. /// <param name="position">发生错误位置</param>
  41. public static void WriteLog(Exception ex, string position, string path)
  42. {
  43. SavePath = path;
  44. EX = ex;
  45. Position = position;
  46. WriteDisk();//文件写入硬盘
  47. }
  48. /// <summary>
  49. /// 向文件中写错误日志,记录错误信息,发生错误位置,错误级别
  50. /// </summary>
  51. /// <param name="ex">错误信息</param>
  52. /// <param name="position">发生错误位置</param>
  53. /// <param name="type">错误级别</param>
  54. public static void WriteLog(Exception ex, string position, string leve, string path)
  55. {
  56. SavePath = path;
  57. EX = ex;
  58. Leve = leve;
  59. Position = position;
  60. WriteDisk();//文件写入硬盘
  61. }
  62. /// <summary>
  63. /// 文件写入硬盘
  64. /// </summary>
  65. /// <param name="str"></param>
  66. private static void WriteDisk()
  67. {
  68. FileStream fs = null;
  69. try
  70. {
  71. string str = "时间:" + System.DateTime.Now.ToString() + "\t";
  72. if (EX != null)
  73. {
  74. str += "错误消息:" + EX.Message + "\t";
  75. if (EX.HelpLink != null)
  76. {
  77. str += "与此关联的文件:" + EX.HelpLink + "\t";
  78. }
  79. str += "错误消息字符串表示:" + EX.StackTrace + "\t";
  80. }
  81. if (Position != null)
  82. {
  83. str += "错误位置:" + Position + "\t";
  84. }
  85. if (Leve != null)
  86. {
  87. str += "错误级别:" + Leve + "\t";
  88. }
  89. str += "\r\n\r\n\r\n";
  90. string path = SavePath + "/Logs/ErrorLogs/" + DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("yyyyMMdd") + "Log.txt";
  91. if (!File.Exists(path))
  92. {
  93. Directory.CreateDirectory(SavePath + "/Logs/ErrorLogs/" + DateTime.Now.ToString("yyyyMM") + "/");
  94. File.Create(path).Close();
  95. }
  96. //字符串写入硬盘
  97. byte[] bytData = System.Text.Encoding.Default.GetBytes(str);
  98. fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 2048);
  99. fs.Write(bytData, 0, bytData.Length);
  100. }
  101. finally
  102. {
  103. if (fs != null)
  104. {
  105. fs.Close();
  106. }
  107. EX = null;
  108. Position = null;
  109. Leve = null;
  110. }
  111. }
  112. /// <summary>
  113. /// 文件写入硬盘 操作日志
  114. /// </summary>
  115. /// <param name="str"></param>
  116. public static void WriteOptDisk(string info, string paths)
  117. {
  118. SavePath = paths;
  119. FileStream fs = null;
  120. try
  121. {
  122. string str = "时间:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ms") + "\t";
  123. str += "操作信息:" + info + "\t";
  124. str += "\r\n\r\n\r\n";
  125. string path = SavePath + "/Logs/OptLogs/" + DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "Log.txt";
  126. if (!File.Exists(path))
  127. {
  128. Directory.CreateDirectory(SavePath + "/Logs/OptLogs/" + DateTime.Now.ToString("yyyyMM")+ "/");
  129. File.Create(path).Close();
  130. }
  131. //字符串写入硬盘
  132. byte[] bytData = System.Text.Encoding.Default.GetBytes(str);
  133. fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 2048);
  134. fs.Write(bytData, 0, bytData.Length);
  135. }
  136. finally
  137. {
  138. if (fs != null)
  139. {
  140. fs.Close();
  141. }
  142. }
  143. }
  144. #endregion
  145. }
  146. }