Log.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace Core.StlMes.Client.LgResMgt.Mcms
  7. {
  8. public class Log
  9. {
  10. /// <summary>
  11. /// 禁止通过new创建实例
  12. /// </summary>
  13. private Log() { }
  14. private static Log log;
  15. // 定义一个标识确保线程同步
  16. private static readonly object locker = new object();
  17. public static Log GetInstance()
  18. {
  19. if (log == null)
  20. {
  21. lock (locker)
  22. {
  23. if (log == null)
  24. {
  25. log = new Log();
  26. }
  27. }
  28. }
  29. return log;
  30. }
  31. /// <summary>
  32. /// 写入日志
  33. /// </summary>
  34. /// <param name="iType">0智能终端 1数据采集 2网络状态 3计量实绩 4计量监控 5远程计量 6静态衡 7动态衡 8成品秤</param>
  35. /// <param name="str"></param>
  36. public void WriteLog(int iType, string str)
  37. {
  38. try
  39. {
  40. string strLogName = "";
  41. switch (iType)
  42. {
  43. case 0:
  44. strLogName = "计量终端_";
  45. break;
  46. case 1:
  47. strLogName = "数据采集_";
  48. break;
  49. case 2:
  50. strLogName = "网络状态_";
  51. break;
  52. case 3:
  53. strLogName = "计量实绩_";
  54. break;
  55. case 4:
  56. strLogName = "计量监控_";
  57. break;
  58. case 5:
  59. strLogName = "远程计量_";
  60. break;
  61. case 6:
  62. strLogName = "静态衡计量_";
  63. break;
  64. case 7:
  65. strLogName = "动态衡计量_";
  66. break;
  67. case 8:
  68. strLogName = "成品计量_";
  69. break;
  70. case 9:
  71. strLogName = "提示信息_";
  72. break;
  73. case 10:
  74. strLogName = "打印日志_";
  75. break;
  76. case 11:
  77. strLogName = "静态衡公共事件_";
  78. break;
  79. case 12:
  80. strLogName = "主线程扫码设备_";
  81. break;
  82. case 13:
  83. strLogName = "tryCatch异常_";
  84. break;
  85. case 14:
  86. strLogName = "保存按钮状态_";
  87. break;
  88. case 15:
  89. strLogName = "按钮点击日志_";
  90. break;
  91. case 16:
  92. strLogName = "服务调用日志_";
  93. break;
  94. case 17:
  95. strLogName = "自动卸货日志_";
  96. break;
  97. case 18:
  98. strLogName = "复磅计量_";
  99. break;
  100. default:
  101. strLogName = "计量终端_";
  102. break;
  103. }
  104. string m_szRunPath;
  105. m_szRunPath = System.Environment.CurrentDirectory;
  106. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  107. {
  108. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  109. }
  110. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  111. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  112. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  113. {
  114. Directory.CreateDirectory(strPathFile);
  115. }
  116. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\" + strLogName + strDate + ".log", true);
  117. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  118. tw.WriteLine(str);
  119. tw.WriteLine("\r\n");
  120. tw.Close();
  121. }
  122. catch (Exception ex)
  123. {
  124. }
  125. }
  126. }
  127. }