FileOperation.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Configuration;
  6. namespace Dongke.IBOSS.PRD.WCF.Hosting
  7. {
  8. public static class LogFileOperation
  9. {
  10. #region 对外使用的公用方法
  11. /// <summary>
  12. /// 以Debug级别输出日志信息
  13. /// </summary>
  14. /// <param name="p_fileName">日志文件名称</param>
  15. /// <param name="p_logType">日志类型 true 记录日志,False 不记录日志</param>
  16. /// <param name="p_info">要输出的信息</param>
  17. public static void Debug(string p_fileName, bool p_logType, string p_info)
  18. {
  19. try
  20. {
  21. if (p_logType)
  22. {
  23. if (!string.IsNullOrEmpty(p_fileName))
  24. {
  25. string strDateTime = GetNowDateTime();
  26. string strFileFullName = GetCreateFile(p_fileName);
  27. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  28. {
  29. sw.WriteLine(strDateTime + "(Debug) - " + p_info);
  30. sw.Close();
  31. }
  32. }
  33. }
  34. }
  35. catch { }
  36. }
  37. /// <summary>
  38. /// 以Info级别输出日志信息
  39. /// </summary>
  40. /// <param name="p_fileName">日志文件名称</param>
  41. /// <param name="p_logType">日志类型 true 记录日志,False 不记录日志</param>
  42. /// <param name="p_info">要输出的信息</param>
  43. public static void Info(string p_fileName, bool p_logType, string p_info)
  44. {
  45. try
  46. {
  47. if (p_logType)
  48. {
  49. if (!string.IsNullOrEmpty(p_fileName))
  50. {
  51. string strDateTime = GetNowDateTime();
  52. string strFileFullName = GetCreateFile(p_fileName);
  53. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  54. {
  55. sw.WriteLine(strDateTime + "(Info) - " + p_info);
  56. sw.Close();
  57. }
  58. }
  59. }
  60. }
  61. catch { }
  62. }
  63. /// <summary>
  64. /// 以Warn级别输出日志信息
  65. /// </summary>
  66. /// <param name="p_fileName">日志文件名称</param>
  67. /// <param name="p_logType">日志类型 true 记录日志,False 不记录日志</param>
  68. /// <param name="p_info">要输出的信息</param>
  69. public static void Warn(string p_fileName, bool p_logType, string p_info)
  70. {
  71. try
  72. {
  73. if (p_logType)
  74. {
  75. if (!string.IsNullOrEmpty(p_fileName))
  76. {
  77. string strDateTime = GetNowDateTime();
  78. string strFileFullName = GetCreateFile(p_fileName);
  79. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  80. {
  81. sw.WriteLine(strDateTime + "(Warn) - " + p_info);
  82. sw.Close();
  83. }
  84. }
  85. }
  86. }
  87. catch { }
  88. }
  89. /// <summary>
  90. /// 以Error级别输出日志信息
  91. /// </summary>
  92. /// <param name="p_fileName">日志文件名称</param>
  93. /// <param name="p_info">要输出的信息</param>
  94. public static void Error(string p_fileName, string p_info)
  95. {
  96. try
  97. {
  98. if (!string.IsNullOrEmpty(p_fileName))
  99. {
  100. string strDateTime = GetNowDateTime();
  101. string strFileFullName = GetCreateFile(p_fileName);
  102. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  103. {
  104. sw.WriteLine(strDateTime + "(Error) - " + p_info);
  105. sw.Close();
  106. }
  107. }
  108. }
  109. catch { }
  110. }
  111. /// <summary>
  112. /// 以Fatal级别输出日志信息
  113. /// </summary>
  114. /// <param name="p_fileName">日志文件名称</param>
  115. /// <param name="p_info">要输出的信息</param>
  116. public static void Fatal(string p_fileName, string p_info)
  117. {
  118. try
  119. {
  120. if (!string.IsNullOrEmpty(p_fileName))
  121. {
  122. string strDateTime = GetNowDateTime();
  123. string strFileFullName = GetCreateFile(p_fileName);
  124. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  125. {
  126. sw.WriteLine(strDateTime + "(Fatal) - " + p_info);
  127. sw.Close();
  128. }
  129. }
  130. }
  131. catch { }
  132. }
  133. /// <summary>
  134. /// 创建SQL语句,覆盖文件
  135. /// </summary>
  136. /// <param name="p_fileName">文件名</param>
  137. /// <param name="p_sql">要保存的SQL语句</param>
  138. public static void CreateSqlFile(string p_fileName, List<string> p_sql)
  139. {
  140. try
  141. {
  142. if (!string.IsNullOrEmpty(p_fileName))
  143. {
  144. DeleteFile(p_fileName);
  145. string strFileFullName = GetCreateFile(p_fileName);
  146. using (StreamWriter sw = new StreamWriter(strFileFullName, false, System.Text.Encoding.UTF8))
  147. {
  148. foreach (string strSql in p_sql)
  149. {
  150. sw.WriteLine(strSql);
  151. }
  152. sw.Close();
  153. }
  154. }
  155. }
  156. catch { }
  157. }
  158. /// <summary>
  159. /// 删除对应的文件
  160. /// </summary>
  161. /// <param name="p_fileName">文件名称</param>
  162. public static void DeleteLogFile(string p_fileName)
  163. {
  164. DeleteFile(p_fileName);
  165. }
  166. /// <summary>
  167. /// 根据文件名获取对应的文件内容,并返回文件内容
  168. /// </summary>
  169. /// <param name="p_fileName"></param>
  170. /// <returns></returns>
  171. public static byte[] ReadFile(string p_fileName)
  172. {
  173. string filePath = ConfigConst.StartupPath + "\\Log\\";
  174. try
  175. {
  176. string strFileName = p_fileName;
  177. if (!p_fileName.Contains("."))
  178. strFileName = p_fileName + ".log";
  179. filePath = filePath + strFileName;
  180. if (File.Exists(filePath))
  181. {
  182. FileStream fsMyfile = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  183. byte[] byteFile = new byte[fsMyfile.Length];
  184. BinaryReader brMyfile = new BinaryReader(fsMyfile, Encoding.UTF8);
  185. brMyfile.Read(byteFile, 0, byteFile.Length);
  186. brMyfile.Close();
  187. fsMyfile.Close();
  188. fsMyfile.Dispose();
  189. return byteFile;
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. throw ex;
  195. }
  196. return System.Text.Encoding.UTF8.GetBytes(filePath + " 日志文件不存在!");
  197. }
  198. /// <summary>
  199. /// 获取文件长度
  200. /// </summary>
  201. /// <param name="p_fileName"></param>
  202. /// <returns></returns>
  203. public static long GetFileSize(string p_fileName)
  204. {
  205. string filePath = ConfigConst.StartupPath + "\\Log\\";
  206. try
  207. {
  208. string strFileName = p_fileName;
  209. if (!p_fileName.Contains("."))
  210. strFileName = p_fileName + ".log";
  211. filePath = filePath + strFileName;
  212. if (File.Exists(filePath))
  213. {
  214. FileStream fsMyfile = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  215. long fileSize = fsMyfile.Length;
  216. fsMyfile.Close();
  217. fsMyfile.Dispose();
  218. return fileSize;
  219. }
  220. }
  221. catch (Exception ex)
  222. {
  223. throw ex;
  224. }
  225. return 0;
  226. }
  227. #endregion
  228. #region 自定义方法
  229. /// <summary>
  230. /// 设置当前时间
  231. /// </summary>
  232. /// <returns>返回系统时间</returns>
  233. private static string GetNowDateTime()
  234. {
  235. DateTime dt = DateTime.Now;
  236. return dt.ToString("yyyy-MM-dd HH:mm:ss,fff");
  237. }
  238. /// <summary>
  239. /// 返回对应的文件全名
  240. /// </summary>
  241. /// <param name="p_fileName"></param>
  242. /// <returns>返回对应的文件全名</returns>
  243. private static string GetCreateFile(string p_fileName)
  244. {
  245. string filePath = ConfigConst.StartupPath + "\\Log\\";
  246. DirectoryInfo dirFile = new DirectoryInfo(filePath);
  247. if (!dirFile.Exists)
  248. {
  249. dirFile.Create();
  250. }
  251. string strFileName = p_fileName;
  252. if (!p_fileName.Contains("."))
  253. {
  254. strFileName = p_fileName + ".log";
  255. }
  256. //if (strFileName.Contains(ConfigConst.LogFileNameOld))
  257. //{
  258. // string strTempFileAllName = filePath + strFileName;
  259. // if (System.IO.File.Exists(strTempFileAllName))
  260. // {
  261. // System.IO.FileInfo fileSize = new System.IO.FileInfo(strTempFileAllName);
  262. // if (fileSize.Length > (20 * 1024 * 1024)) //大于20M
  263. // {
  264. // strFileName = ConfigConst.LogFileNameOld + DateTime.Now.ToString("yyyyMMddHHmmss");
  265. // ConfigConst.LogFileName = strFileName;
  266. // strFileName = strFileName + ".log";
  267. // }
  268. // }
  269. //}
  270. return filePath + strFileName;
  271. }
  272. /// <summary>
  273. /// 删除日志文件信息
  274. /// </summary>
  275. /// <param name="p_fileName"></param>
  276. private static void DeleteFile(string p_fileName)
  277. {
  278. try
  279. {
  280. string filePath = ConfigConst.StartupPath + "\\Log\\";
  281. string strFileName = p_fileName;
  282. if (!p_fileName.Contains("."))
  283. strFileName = p_fileName + ".log";
  284. filePath = filePath + strFileName;
  285. if (File.Exists(filePath))
  286. System.IO.File.Delete(filePath);
  287. }
  288. catch { }
  289. }
  290. #endregion
  291. }
  292. }