| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace FrmStandAloneMetering
- {
- public class logCsv
- {
- private static LogManager logManager;
- static logCsv()
- {
- logManager = new LogManager();
- }
- /// <summary>
- /// 数据的日志文件
- /// </summary>
- /// <param name="logFile"></param>
- /// <param name="msg"></param>
- public static void WriteDataLog(string logFile, string msg)
- {
- try
- {
- logManager.WriteDataLog(logFile, msg);
- }
- catch
- {
- }
- }
- /// <summary>
- /// 基础的日志文件(物资、收发货单位等)
- /// </summary>
- /// <param name="logFile"></param>
- /// <param name="msg"></param>
- public static void WriteBaseData(string logFile, string msg)
- {
- try
- {
- logManager.WriteBaseData(logFile, msg);
- }
- catch
- {
- }
- }
- public static void WriteLog(LogFile logFile, string msg)
- {
- try
- {
- logManager.WriteLog(logFile, msg);
- }
- catch
- {
- }
- }
- public static void WriteLog(string msg)
- {
- try
- {
- logManager.WriteLog(LogFile.Info, msg);
- }
- catch
- {
- }
- }
- public static void WriteLog(string logFile, string msg)
- {
- try
- {
- logManager.WriteLog(logFile, msg);
- }
- catch
- {
- }
- }
- }
- public class LogManager
- {
- private string logFileName = string.Empty;
- private string logPath = "Log";
- private string logFileExtName = "log";
- private bool writeLogTime = true;
- private bool logFileNameEndWithDate = true;
- private Encoding logFileEncoding = Encoding.UTF8;
- private object obj = new object();
- #region 构造函数
- public LogManager()
- {
- this.LogPath = "Log";
- this.LogFileExtName = "csv";
- this.WriteLogTime = true;
- this.logFileNameEndWithDate = true;
- this.logFileEncoding = Encoding.UTF8;
- }
- public LogManager(string logPath, string logFileExtName, bool writeLogTime)
- {
- this.LogPath = logPath;
- this.LogFileExtName = logFileExtName;
- this.WriteLogTime = writeLogTime;
- this.logFileNameEndWithDate = true;
- this.logFileEncoding = Encoding.UTF8;
- }
- #endregion
- #region 属性
- /// <summary>
- /// Log 文件路径
- /// </summary>
- public string LogPath
- {
- get
- {
- if (this.logPath == null || this.logPath == string.Empty)
- {
- //Application.StartupPath
- this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
- }
- return this.logPath;
- }
- set
- {
- this.logPath = value;
- if (this.logPath == null || this.logPath == string.Empty)
- {
- //Application.StartupPath
- this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
- }
- else
- {
- try
- {
- // 判断是否不是绝对路径(绝对路径里还有":")
- if (this.logPath.IndexOf(Path.VolumeSeparatorChar) >= 0)
- { /* 绝对路径 */}
- else
- {
- // 相对路径
- this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + this.logPath, DateTime.Now.ToString("yyyy-MM-dd"));
- }
- if (!Directory.Exists(this.logPath))
- Directory.CreateDirectory(this.logPath);
- }
- catch
- {
- this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
- }
- if (!this.logPath.EndsWith(@"\"))
- this.logPath += @"\";
- }
- }
- }
- /// <summary>
- /// Log 文件扩展名
- /// </summary>
- public string LogFileExtName
- {
- get { return this.logFileExtName; }
- set { this.logFileExtName = value; }
- }
- /// <summary>
- /// 是否在每个Log行前面添加当前时间
- /// </summary>
- public bool WriteLogTime
- {
- get { return this.writeLogTime; }
- set { this.writeLogTime = value; }
- }
- /// <summary>
- /// 日志文件名是否带日期
- /// </summary>
- public bool LogFileNameEndWithDate
- {
- get { return logFileNameEndWithDate; }
- set { logFileNameEndWithDate = value; }
- }
- /// <summary>
- /// 日志文件的字符编码
- /// </summary>
- public Encoding LogFileEncoding
- {
- get { return logFileEncoding; }
- set { logFileEncoding = value; }
- }
- #endregion
- #region 公有方法
- public void WriteDataLog(string logFile, string msg)
- {
- lock (obj)
- {
- try
- {
- this.LogPath = "alonData";
- logFileName = string.Format("{0}{1}.{2}",
- this.LogPath, //文件夹带年月日时分秒
- logFile,
- this.logFileExtName);
- using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
- {
- sw.WriteLine(msg);
- }
- }
- catch
- {
- }
- }
- }
- /// <summary>
- /// 基础数据下载
- /// </summary>
- /// <param name="logFile"></param>
- /// <param name="msg"></param>
- public void WriteBaseData(string logFile, string msg)
- {
- lock (obj)
- {
- try
- {
- string basePath = AppDomain.CurrentDomain.BaseDirectory;
- logFileName = string.Format("{0}{1}.{2}",
- //this.LogPath,
- basePath + "baseData//",
- logFile,
- this.logFileExtName);
- using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
- {
- sw.WriteLine(msg);
- }
- }
- catch(Exception exp)
- {
- string str = exp.ToString();
- }
- }
- }
- public void WriteLog(string logFile, string msg)
- {
- lock (obj)
- {
- try
- {
- this.LogPath = "Log";
- logFileName = string.Format("{0}{1}.{2}",
- this.LogPath,
- logFile,
- this.logFileExtName);
- using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
- {
- if (writeLogTime)
- {
- sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + msg);
- }
- else
- {
- sw.WriteLine(msg);
- }
- }
- }
- catch
- {
- }
- }
- }
- /// <summary>
- /// 一个小时一个文件名,格式为:actualFirst2021090111
- /// </summary>
- /// <param name="logFile"></param>
- /// <param name="msg"></param>
- public void WriteLogHour(string logFile, string msg)
- {
- lock (obj)
- {
- try
- {
- string dateString = string.Empty;
- if (this.logFileNameEndWithDate || logFile.Length == 0)
- {
- dateString = DateTime.Now.ToString("yyyyMMddHH");
- }
- this.LogPath = "Log";
- logFileName = string.Format("{0}{1}{2}.{3}",
- this.LogPath,
- logFile,
- dateString,
- this.logFileExtName);
- using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
- {
- if (writeLogTime)
- {
- sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + msg);
- }
- else
- {
- sw.WriteLine(msg);
- }
- }
- }
- catch
- {
- }
- }
- }
- public void WriteLog(LogFile logFile, string msg)
- {
- this.WriteLog(logFile.ToString(), msg);
- }
- public void WriteDataLog(LogFile logFile, string msg)
- {
- this.WriteDataLog(logFile.ToString(), msg);
- }
- public void WriteLog(string msg)
- {
- this.WriteLog(string.Empty, msg);
- }
- #endregion
- }
- public enum LogFile
- {
- Trace,
- Error,
- SQL,
- SQLError,
- Login,
- Info,
- WeChat
- }
- }
|