飞依诺接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.3 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. namespace ICSSoft.ERPWMS.SQL
  7. {
  8. public class Log
  9. {
  10. public static void WriteLogFile(string input, string txtName)
  11. {
  12. try
  13. {
  14. string logAdress = "D:" + "\\Log\\";
  15. if (!System.IO.Directory.Exists(logAdress))
  16. {
  17. System.IO.Directory.CreateDirectory(logAdress);//不存在就创建目录
  18. }
  19. string adress = logAdress + txtName;
  20. if (!System.IO.Directory.Exists(adress))
  21. {
  22. System.IO.Directory.CreateDirectory(adress);//不存在就创建目录
  23. }
  24. // string logAdress = ConfigurationManager.AppSettings["logAdress"].ToString();
  25. /**/
  26. ///指定日志文件的目录
  27. string fname = adress + "\\" + "log" + DateTime.Now.ToString("yy-MM-dd") + ".txt";
  28. /**/
  29. ///定义文件信息对象
  30. FileInfo finfo = new FileInfo(fname);
  31. if (!finfo.Exists)
  32. {
  33. FileStream fs;
  34. fs = File.Create(fname);
  35. fs.Close();
  36. finfo = new FileInfo(fname);
  37. }
  38. /**/
  39. ///判断文件是否存在以及是否大于2K
  40. if (finfo.Length > 1024 * 1024 * 10)
  41. {
  42. /**/
  43. ///文件超过10MB则重命名
  44. File.Move(logAdress + "\\Log\\" + txtName + ".txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\Log\\" + txtName + ".txt");
  45. /**/
  46. ///删除该文件
  47. //finfo.Delete();
  48. }
  49. //finfo.AppendText();
  50. /**/
  51. ///创建只写文件流
  52. using (FileStream fs = finfo.OpenWrite())
  53. {
  54. /**/
  55. ///根据上面创建的文件流创建写数据流
  56. StreamWriter w = new StreamWriter(fs);
  57. /**/
  58. ///设置写数据流的起始位置为文件流的末尾
  59. ///设置写数据流的起始位置为文件流的末尾
  60. w.BaseStream.Seek(0, SeekOrigin.End);
  61. w.WriteLine("*****************Start*****************");
  62. w.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  63. /**/
  64. ///写入当前系统时间并换行
  65. /**/
  66. ///写入日志内容并换行
  67. w.WriteLine(input);
  68. /**/
  69. ///写入------------------------------------“并换行
  70. w.WriteLine("------------------END------------------------");
  71. /**/
  72. ///清空缓冲区内容,并把缓冲区内容写入基础流
  73. w.Flush();
  74. /**/
  75. ///关闭写数据流
  76. w.Close();
  77. }
  78. }
  79. catch (Exception ex)
  80. { }
  81. }
  82. }
  83. }