| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Core.StlMes.Client.LgResMgt.Mcms
- {
- public class Log
- {
- /// <summary>
- /// 禁止通过new创建实例
- /// </summary>
- private Log() { }
- private static Log log;
- // 定义一个标识确保线程同步
- private static readonly object locker = new object();
- public static Log GetInstance()
- {
- if (log == null)
- {
- lock (locker)
- {
- if (log == null)
- {
- log = new Log();
- }
- }
- }
- return log;
- }
- /// <summary>
- /// 写入日志
- /// </summary>
- /// <param name="iType">0智能终端 1数据采集 2网络状态 3计量实绩 4计量监控 5远程计量 6静态衡 7动态衡 8成品秤</param>
- /// <param name="str"></param>
- public void WriteLog(int iType, string str)
- {
- try
- {
- string strLogName = "";
- switch (iType)
- {
- case 0:
- strLogName = "计量终端_";
- break;
- case 1:
- strLogName = "数据采集_";
- break;
- case 2:
- strLogName = "网络状态_";
- break;
- case 3:
- strLogName = "计量实绩_";
- break;
- case 4:
- strLogName = "计量监控_";
- break;
- case 5:
- strLogName = "远程计量_";
- break;
- case 6:
- strLogName = "静态衡计量_";
- break;
- case 7:
- strLogName = "动态衡计量_";
- break;
- case 8:
- strLogName = "成品计量_";
- break;
- case 9:
- strLogName = "提示信息_";
- break;
- case 10:
- strLogName = "打印日志_";
- break;
- case 11:
- strLogName = "静态衡公共事件_";
- break;
- case 12:
- strLogName = "主线程扫码设备_";
- break;
- case 13:
- strLogName = "tryCatch异常_";
- break;
- case 14:
- strLogName = "保存按钮状态_";
- break;
- case 15:
- strLogName = "按钮点击日志_";
- break;
- case 16:
- strLogName = "服务调用日志_";
- break;
- case 17:
- strLogName = "自动卸货日志_";
- break;
- case 18:
- strLogName = "复磅计量_";
- break;
- default:
- strLogName = "计量终端_";
- break;
- }
- string m_szRunPath;
- m_szRunPath = System.Environment.CurrentDirectory;
- if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
- {
- System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
- }
- string strDate = System.DateTime.Now.ToString("yyyyMMdd");
- string strPathFile = m_szRunPath + "\\log\\" + strDate;
- if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
- {
- Directory.CreateDirectory(strPathFile);
- }
- System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\" + strLogName + strDate + ".log", true);
- tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- tw.WriteLine(str);
- tw.WriteLine("\r\n");
- tw.Close();
- }
- catch (Exception ex)
- {
- }
- }
- }
- }
|