Log.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. namespace WCS_Client
  5. {
  6. public class Log
  7. {
  8. /// 日志文件的路径
  9. private string fileLogPath;
  10. /// 日志文件的名称
  11. private string dataFileName;
  12. public Log(string FileName, string filepath)
  13. {
  14. this.fileLogPath = filepath;
  15. this.dataFileName = FileName + ".txt";
  16. //this.dataFileName = DateTime.Now.ToString("yyyyMMdd") + FileName + ".txt";
  17. }
  18. /// <summary>
  19. /// 获取或设置日志文件的路径
  20. /// </summary>
  21. public string FileLogPath
  22. {
  23. set
  24. {
  25. this.fileLogPath = value;
  26. }
  27. get
  28. {
  29. return this.fileLogPath;
  30. }
  31. }
  32. /// <summary>
  33. /// 获取或设置日志文件的名称
  34. /// </summary>
  35. public string DataFileName
  36. {
  37. set
  38. {
  39. this.dataFileName = value;
  40. }
  41. get
  42. {
  43. return this.dataFileName;
  44. }
  45. }
  46. /// <summary>
  47. /// 向指定目录中的指定的文件中追加日志文件
  48. /// </summary>
  49. /// <param name="Message">要写入的内容</param>
  50. public void WriteLog(string Message)
  51. {
  52. this.WriteLog(this.dataFileName, Message);
  53. }
  54. /// <summary>
  55. /// 向指定目录中的文件中追加日志文件,日志文件的名称将由传递的参数决定.
  56. /// </summary>
  57. /// <param name="LogFileName">日志文件的名称,如:mylog.txt ,如果没有自动创建,如果存在将追加写入日志</param>
  58. /// <param name="Message">要写入的内容</param>
  59. public void WriteLog(string LogFileName, string Message)
  60. {
  61. //如果日志文件目录不存在,则创建
  62. if (!Directory.Exists(this.fileLogPath))
  63. {
  64. Directory.CreateDirectory(this.fileLogPath);
  65. }
  66. FileInfo finfo = new FileInfo(this.fileLogPath + LogFileName);
  67. try
  68. {
  69. FileStream fs = new FileStream(this.fileLogPath + LogFileName, FileMode.Append);
  70. StreamWriter strwriter = new StreamWriter(fs);
  71. try
  72. {
  73. strwriter.WriteLine(System.DateTime.Now.ToString() + " " + DateTime.Now.Millisecond.ToString().PadLeft(3, '0') + ": " + Message);
  74. strwriter.Flush();
  75. }
  76. catch (Exception ee)
  77. {
  78. Console.WriteLine("日志文件写入失败信息:" + ee.ToString());
  79. }
  80. finally
  81. {
  82. strwriter.Close();
  83. strwriter = null;
  84. fs.Close();
  85. fs = null;
  86. }
  87. }
  88. catch (Exception ee)
  89. {
  90. Console.WriteLine("日志文件没有打开,详细信息如下:" + ee.ToString());
  91. }
  92. }
  93. }
  94. public class LogHelper
  95. {
  96. //private static Log _sys_Log = new Log("系统", "C:\\webapi日志\\");//Application.StartupPath +
  97. //private static string mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
  98. private static string mappedPath = Application.StartupPath;
  99. private static string dt = DateTime.Now.ToString("yyyyMMdd");
  100. private static string fileName = dt + " 系统";
  101. private static Log _sys_Log = new Log(fileName, mappedPath + "\\Logs\\");
  102. public static Log Sys_Log
  103. {
  104. get
  105. {
  106. if (dt != DateTime.Now.ToString("yyyyMMdd"))
  107. {
  108. dt = DateTime.Now.ToString("yyyyMMdd");
  109. _sys_Log = new Log(fileName, mappedPath + "\\Logs\\");
  110. }
  111. return _sys_Log;
  112. }
  113. }
  114. }
  115. }