FileOperation.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.WindowsService
  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. public static void ErrorFile(string p_fileName, string p_info)
  112. {
  113. try
  114. {
  115. if (!string.IsNullOrEmpty(p_fileName))
  116. {
  117. string strDateTime = GetNowDateTime();
  118. string strFileFullName = p_fileName;
  119. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  120. {
  121. sw.WriteLine(strDateTime + "(Error) - " + p_info);
  122. sw.Close();
  123. }
  124. }
  125. }
  126. catch
  127. {
  128. }
  129. }
  130. /// <summary>
  131. /// 以Fatal级别输出日志信息
  132. /// </summary>
  133. /// <param name="p_fileName">日志文件名称</param>
  134. /// <param name="p_info">要输出的信息</param>
  135. public static void Fatal(string p_fileName, string p_info)
  136. {
  137. try
  138. {
  139. if (!string.IsNullOrEmpty(p_fileName))
  140. {
  141. string strDateTime = GetNowDateTime();
  142. string strFileFullName = GetCreateFile(p_fileName);
  143. using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
  144. {
  145. sw.WriteLine(strDateTime + "(Fatal) - " + p_info);
  146. sw.Close();
  147. }
  148. }
  149. }
  150. catch { }
  151. }
  152. /// <summary>
  153. /// 创建SQL语句,覆盖文件
  154. /// </summary>
  155. /// <param name="p_fileName">文件名</param>
  156. /// <param name="p_sql">要保存的SQL语句</param>
  157. public static void CreateSqlFile(string p_fileName, List<string> p_sql)
  158. {
  159. try
  160. {
  161. if (!string.IsNullOrEmpty(p_fileName))
  162. {
  163. DeleteFile(p_fileName);
  164. string strFileFullName = GetCreateFile(p_fileName);
  165. using (StreamWriter sw = new StreamWriter(strFileFullName, false, System.Text.Encoding.UTF8))
  166. {
  167. foreach (string strSql in p_sql)
  168. {
  169. sw.WriteLine(strSql);
  170. }
  171. sw.Close();
  172. }
  173. }
  174. }
  175. catch { }
  176. }
  177. /// <summary>
  178. /// 删除对应的文件
  179. /// </summary>
  180. /// <param name="p_fileName">文件名称</param>
  181. public static void DeleteLogFile(string p_fileName)
  182. {
  183. DeleteFile(p_fileName);
  184. }
  185. /// <summary>
  186. /// 根据文件名获取对应的文件内容,并返回文件内容
  187. /// </summary>
  188. /// <param name="p_fileName"></param>
  189. /// <returns></returns>
  190. public static byte[] ReadFile(string p_fileName)
  191. {
  192. string filePath = ConfigConst.StartupPath + "\\Log\\";
  193. try
  194. {
  195. string strFileName = p_fileName;
  196. if (!p_fileName.Contains("."))
  197. strFileName = p_fileName + ".log";
  198. filePath = filePath + strFileName;
  199. if (File.Exists(filePath))
  200. {
  201. FileStream fsMyfile = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  202. byte[] byteFile = new byte[fsMyfile.Length];
  203. BinaryReader brMyfile = new BinaryReader(fsMyfile, Encoding.UTF8);
  204. brMyfile.Read(byteFile, 0, byteFile.Length);
  205. brMyfile.Close();
  206. fsMyfile.Close();
  207. fsMyfile.Dispose();
  208. return byteFile;
  209. }
  210. }
  211. catch (Exception ex)
  212. {
  213. throw ex;
  214. }
  215. return System.Text.Encoding.UTF8.GetBytes(filePath + " 日志文件不存在!");
  216. }
  217. /// <summary>
  218. /// 获取文件长度
  219. /// </summary>
  220. /// <param name="p_fileName"></param>
  221. /// <returns></returns>
  222. public static long GetFileSize(string p_fileName)
  223. {
  224. string filePath = ConfigConst.StartupPath + "\\Log\\";
  225. try
  226. {
  227. string strFileName = p_fileName;
  228. if (!p_fileName.Contains("."))
  229. strFileName = p_fileName + ".log";
  230. filePath = filePath + strFileName;
  231. if (File.Exists(filePath))
  232. {
  233. FileStream fsMyfile = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  234. long fileSize = fsMyfile.Length;
  235. fsMyfile.Close();
  236. fsMyfile.Dispose();
  237. return fileSize;
  238. }
  239. }
  240. catch (Exception ex)
  241. {
  242. throw ex;
  243. }
  244. return 0;
  245. }
  246. #endregion
  247. #region 自定义方法
  248. /// <summary>
  249. /// 设置当前时间
  250. /// </summary>
  251. /// <returns>返回系统时间</returns>
  252. private static string GetNowDateTime()
  253. {
  254. DateTime dt = DateTime.Now;
  255. return dt.ToString("yyyy-MM-dd HH:mm:ss,fff");
  256. }
  257. /// <summary>
  258. /// 返回对应的文件全名
  259. /// </summary>
  260. /// <param name="p_fileName"></param>
  261. /// <returns>返回对应的文件全名</returns>
  262. private static string GetCreateFile(string p_fileName)
  263. {
  264. string filePath = ConfigConst.StartupPath + "\\Log\\";
  265. DirectoryInfo dirFile = new DirectoryInfo(filePath);
  266. if (!dirFile.Exists)
  267. {
  268. dirFile.Create();
  269. }
  270. string strFileName = p_fileName;
  271. if (!p_fileName.Contains("."))
  272. {
  273. strFileName = p_fileName + ".log";
  274. }
  275. //if (strFileName.Contains(ConfigConst.LogFileNameOld))
  276. //{
  277. // string strTempFileAllName = filePath + strFileName;
  278. // if (System.IO.File.Exists(strTempFileAllName))
  279. // {
  280. // System.IO.FileInfo fileSize = new System.IO.FileInfo(strTempFileAllName);
  281. // if (fileSize.Length > (20 * 1024 * 1024)) //大于20M
  282. // {
  283. // strFileName = ConfigConst.LogFileNameOld + DateTime.Now.ToString("yyyyMMddHHmmss");
  284. // ConfigConst.LogFileName = strFileName;
  285. // strFileName = strFileName + ".log";
  286. // }
  287. // }
  288. //}
  289. return filePath + strFileName;
  290. }
  291. /// <summary>
  292. /// 删除日志文件信息
  293. /// </summary>
  294. /// <param name="p_fileName"></param>
  295. private static void DeleteFile(string p_fileName)
  296. {
  297. try
  298. {
  299. string filePath = ConfigConst.StartupPath + "\\Log\\";
  300. string strFileName = p_fileName;
  301. if (!p_fileName.Contains("."))
  302. strFileName = p_fileName + ".log";
  303. filePath = filePath + strFileName;
  304. if (File.Exists(filePath))
  305. System.IO.File.Delete(filePath);
  306. }
  307. catch { }
  308. }
  309. #endregion
  310. }
  311. }