logCsv.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace FrmStandAloneMetering
  7. {
  8. public class logCsv
  9. {
  10. private static LogManager logManager;
  11. static logCsv()
  12. {
  13. logManager = new LogManager();
  14. }
  15. /// <summary>
  16. /// 数据的日志文件
  17. /// </summary>
  18. /// <param name="logFile"></param>
  19. /// <param name="msg"></param>
  20. public static void WriteDataLog(string logFile, string msg)
  21. {
  22. try
  23. {
  24. logManager.WriteDataLog(logFile, msg);
  25. }
  26. catch
  27. {
  28. }
  29. }
  30. /// <summary>
  31. /// 基础的日志文件(物资、收发货单位等)
  32. /// </summary>
  33. /// <param name="logFile"></param>
  34. /// <param name="msg"></param>
  35. public static void WriteBaseData(string logFile, string msg)
  36. {
  37. try
  38. {
  39. logManager.WriteBaseData(logFile, msg);
  40. }
  41. catch
  42. {
  43. }
  44. }
  45. public static void WriteLog(LogFile logFile, string msg)
  46. {
  47. try
  48. {
  49. logManager.WriteLog(logFile, msg);
  50. }
  51. catch
  52. {
  53. }
  54. }
  55. public static void WriteLog(string msg)
  56. {
  57. try
  58. {
  59. logManager.WriteLog(LogFile.Info, msg);
  60. }
  61. catch
  62. {
  63. }
  64. }
  65. public static void WriteLog(string logFile, string msg)
  66. {
  67. try
  68. {
  69. logManager.WriteLog(logFile, msg);
  70. }
  71. catch
  72. {
  73. }
  74. }
  75. }
  76. public class LogManager
  77. {
  78. private string logFileName = string.Empty;
  79. private string logPath = "Log";
  80. private string logFileExtName = "log";
  81. private bool writeLogTime = true;
  82. private bool logFileNameEndWithDate = true;
  83. private Encoding logFileEncoding = Encoding.UTF8;
  84. private object obj = new object();
  85. #region 构造函数
  86. public LogManager()
  87. {
  88. this.LogPath = "Log";
  89. this.LogFileExtName = "csv";
  90. this.WriteLogTime = true;
  91. this.logFileNameEndWithDate = true;
  92. this.logFileEncoding = Encoding.UTF8;
  93. }
  94. public LogManager(string logPath, string logFileExtName, bool writeLogTime)
  95. {
  96. this.LogPath = logPath;
  97. this.LogFileExtName = logFileExtName;
  98. this.WriteLogTime = writeLogTime;
  99. this.logFileNameEndWithDate = true;
  100. this.logFileEncoding = Encoding.UTF8;
  101. }
  102. #endregion
  103. #region 属性
  104. /// <summary>
  105. /// Log 文件路径
  106. /// </summary>
  107. public string LogPath
  108. {
  109. get
  110. {
  111. if (this.logPath == null || this.logPath == string.Empty)
  112. {
  113. //Application.StartupPath
  114. this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
  115. }
  116. return this.logPath;
  117. }
  118. set
  119. {
  120. this.logPath = value;
  121. if (this.logPath == null || this.logPath == string.Empty)
  122. {
  123. //Application.StartupPath
  124. this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
  125. }
  126. else
  127. {
  128. try
  129. {
  130. // 判断是否不是绝对路径(绝对路径里还有":")
  131. if (this.logPath.IndexOf(Path.VolumeSeparatorChar) >= 0)
  132. { /* 绝对路径 */}
  133. else
  134. {
  135. // 相对路径
  136. this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + this.logPath, DateTime.Now.ToString("yyyy-MM-dd"));
  137. }
  138. if (!Directory.Exists(this.logPath))
  139. Directory.CreateDirectory(this.logPath);
  140. }
  141. catch
  142. {
  143. this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
  144. }
  145. if (!this.logPath.EndsWith(@"\"))
  146. this.logPath += @"\";
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// Log 文件扩展名
  152. /// </summary>
  153. public string LogFileExtName
  154. {
  155. get { return this.logFileExtName; }
  156. set { this.logFileExtName = value; }
  157. }
  158. /// <summary>
  159. /// 是否在每个Log行前面添加当前时间
  160. /// </summary>
  161. public bool WriteLogTime
  162. {
  163. get { return this.writeLogTime; }
  164. set { this.writeLogTime = value; }
  165. }
  166. /// <summary>
  167. /// 日志文件名是否带日期
  168. /// </summary>
  169. public bool LogFileNameEndWithDate
  170. {
  171. get { return logFileNameEndWithDate; }
  172. set { logFileNameEndWithDate = value; }
  173. }
  174. /// <summary>
  175. /// 日志文件的字符编码
  176. /// </summary>
  177. public Encoding LogFileEncoding
  178. {
  179. get { return logFileEncoding; }
  180. set { logFileEncoding = value; }
  181. }
  182. #endregion
  183. #region 公有方法
  184. public void WriteDataLog(string logFile, string msg)
  185. {
  186. lock (obj)
  187. {
  188. try
  189. {
  190. this.LogPath = "alonData";
  191. logFileName = string.Format("{0}{1}.{2}",
  192. this.LogPath, //文件夹带年月日时分秒
  193. logFile,
  194. this.logFileExtName);
  195. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  196. {
  197. sw.WriteLine(msg);
  198. }
  199. }
  200. catch
  201. {
  202. }
  203. }
  204. }
  205. /// <summary>
  206. /// 基础数据下载
  207. /// </summary>
  208. /// <param name="logFile"></param>
  209. /// <param name="msg"></param>
  210. public void WriteBaseData(string logFile, string msg)
  211. {
  212. lock (obj)
  213. {
  214. try
  215. {
  216. string basePath = AppDomain.CurrentDomain.BaseDirectory;
  217. logFileName = string.Format("{0}{1}.{2}",
  218. //this.LogPath,
  219. basePath + "baseData//",
  220. logFile,
  221. this.logFileExtName);
  222. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  223. {
  224. sw.WriteLine(msg);
  225. }
  226. }
  227. catch(Exception exp)
  228. {
  229. string str = exp.ToString();
  230. }
  231. }
  232. }
  233. public void WriteLog(string logFile, string msg)
  234. {
  235. lock (obj)
  236. {
  237. try
  238. {
  239. this.LogPath = "Log";
  240. logFileName = string.Format("{0}{1}.{2}",
  241. this.LogPath,
  242. logFile,
  243. this.logFileExtName);
  244. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  245. {
  246. if (writeLogTime)
  247. {
  248. sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + msg);
  249. }
  250. else
  251. {
  252. sw.WriteLine(msg);
  253. }
  254. }
  255. }
  256. catch
  257. {
  258. }
  259. }
  260. }
  261. /// <summary>
  262. /// 一个小时一个文件名,格式为:actualFirst2021090111
  263. /// </summary>
  264. /// <param name="logFile"></param>
  265. /// <param name="msg"></param>
  266. public void WriteLogHour(string logFile, string msg)
  267. {
  268. lock (obj)
  269. {
  270. try
  271. {
  272. string dateString = string.Empty;
  273. if (this.logFileNameEndWithDate || logFile.Length == 0)
  274. {
  275. dateString = DateTime.Now.ToString("yyyyMMddHH");
  276. }
  277. this.LogPath = "Log";
  278. logFileName = string.Format("{0}{1}{2}.{3}",
  279. this.LogPath,
  280. logFile,
  281. dateString,
  282. this.logFileExtName);
  283. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  284. {
  285. if (writeLogTime)
  286. {
  287. sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + msg);
  288. }
  289. else
  290. {
  291. sw.WriteLine(msg);
  292. }
  293. }
  294. }
  295. catch
  296. {
  297. }
  298. }
  299. }
  300. public void WriteLog(LogFile logFile, string msg)
  301. {
  302. this.WriteLog(logFile.ToString(), msg);
  303. }
  304. public void WriteDataLog(LogFile logFile, string msg)
  305. {
  306. this.WriteDataLog(logFile.ToString(), msg);
  307. }
  308. public void WriteLog(string msg)
  309. {
  310. this.WriteLog(string.Empty, msg);
  311. }
  312. #endregion
  313. }
  314. public enum LogFile
  315. {
  316. Trace,
  317. Error,
  318. SQL,
  319. SQLError,
  320. Login,
  321. Info,
  322. WeChat
  323. }
  324. }