logCsv.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. public string LogPathBase
  151. {
  152. get
  153. {
  154. if (this.logPath == null || this.logPath == string.Empty)
  155. {
  156. //Application.StartupPath
  157. this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
  158. }
  159. return this.logPath;
  160. }
  161. set
  162. {
  163. this.logPath = value;
  164. if (this.logPath == null || this.logPath == string.Empty)
  165. {
  166. //Application.StartupPath
  167. this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
  168. }
  169. else
  170. {
  171. try
  172. {
  173. // 判断是否不是绝对路径(绝对路径里还有":")
  174. if (this.logPath.IndexOf(Path.VolumeSeparatorChar) >= 0)
  175. { /* 绝对路径 */}
  176. else
  177. {
  178. // 相对路径
  179. this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + this.logPath);
  180. }
  181. if (!Directory.Exists(this.logPath))
  182. Directory.CreateDirectory(this.logPath);
  183. }
  184. catch
  185. {
  186. this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
  187. }
  188. if (!this.logPath.EndsWith(@"\"))
  189. this.logPath += @"\";
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// Log 文件扩展名
  195. /// </summary>
  196. public string LogFileExtName
  197. {
  198. get { return this.logFileExtName; }
  199. set { this.logFileExtName = value; }
  200. }
  201. /// <summary>
  202. /// 是否在每个Log行前面添加当前时间
  203. /// </summary>
  204. public bool WriteLogTime
  205. {
  206. get { return this.writeLogTime; }
  207. set { this.writeLogTime = value; }
  208. }
  209. /// <summary>
  210. /// 日志文件名是否带日期
  211. /// </summary>
  212. public bool LogFileNameEndWithDate
  213. {
  214. get { return logFileNameEndWithDate; }
  215. set { logFileNameEndWithDate = value; }
  216. }
  217. /// <summary>
  218. /// 日志文件的字符编码
  219. /// </summary>
  220. public Encoding LogFileEncoding
  221. {
  222. get { return logFileEncoding; }
  223. set { logFileEncoding = value; }
  224. }
  225. #endregion
  226. #region 公有方法
  227. public void WriteDataLog(string logFile, string msg)
  228. {
  229. lock (obj)
  230. {
  231. try
  232. {
  233. this.LogPath = "alonData";
  234. logFileName = string.Format("{0}{1}.{2}",
  235. this.LogPath, //文件夹带年月日时分秒
  236. logFile,
  237. this.logFileExtName);
  238. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  239. {
  240. sw.WriteLine(msg);
  241. }
  242. }
  243. catch
  244. {
  245. }
  246. }
  247. }
  248. /// <summary>
  249. /// 基础数据下载
  250. /// </summary>
  251. /// <param name="logFile"></param>
  252. /// <param name="msg"></param>
  253. public void WriteBaseData(string logFile, string msg)
  254. {
  255. lock (obj)
  256. {
  257. try
  258. {
  259. this.LogPathBase = "baseData";
  260. logFileName = string.Format("{0}{1}.{2}",
  261. this.LogPathBase, //文件夹带年月日时分秒
  262. logFile,
  263. this.logFileExtName);
  264. //string basePath = AppDomain.CurrentDomain.BaseDirectory;
  265. //logFileName = string.Format("{0}{1}.{2}",
  266. // //this.LogPath,
  267. // basePath + "baseData//",
  268. // logFile,
  269. // this.logFileExtName);
  270. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  271. {
  272. sw.WriteLine(msg);
  273. }
  274. }
  275. catch(Exception exp)
  276. {
  277. string str = exp.ToString();
  278. }
  279. }
  280. }
  281. public void WriteLog(string logFile, string msg)
  282. {
  283. lock (obj)
  284. {
  285. try
  286. {
  287. this.LogPath = "Log";
  288. logFileName = string.Format("{0}{1}.{2}",
  289. this.LogPath,
  290. logFile,
  291. this.logFileExtName);
  292. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  293. {
  294. if (writeLogTime)
  295. {
  296. sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + msg);
  297. }
  298. else
  299. {
  300. sw.WriteLine(msg);
  301. }
  302. }
  303. }
  304. catch
  305. {
  306. }
  307. }
  308. }
  309. /// <summary>
  310. /// 一个小时一个文件名,格式为:actualFirst2021090111
  311. /// </summary>
  312. /// <param name="logFile"></param>
  313. /// <param name="msg"></param>
  314. public void WriteLogHour(string logFile, string msg)
  315. {
  316. lock (obj)
  317. {
  318. try
  319. {
  320. string dateString = string.Empty;
  321. if (this.logFileNameEndWithDate || logFile.Length == 0)
  322. {
  323. dateString = DateTime.Now.ToString("yyyyMMddHH");
  324. }
  325. this.LogPath = "Log";
  326. logFileName = string.Format("{0}{1}{2}.{3}",
  327. this.LogPath,
  328. logFile,
  329. dateString,
  330. this.logFileExtName);
  331. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  332. {
  333. if (writeLogTime)
  334. {
  335. sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + msg);
  336. }
  337. else
  338. {
  339. sw.WriteLine(msg);
  340. }
  341. }
  342. }
  343. catch
  344. {
  345. }
  346. }
  347. }
  348. public void WriteLog(LogFile logFile, string msg)
  349. {
  350. this.WriteLog(logFile.ToString(), msg);
  351. }
  352. public void WriteDataLog(LogFile logFile, string msg)
  353. {
  354. this.WriteDataLog(logFile.ToString(), msg);
  355. }
  356. public void WriteLog(string msg)
  357. {
  358. this.WriteLog(string.Empty, msg);
  359. }
  360. #endregion
  361. }
  362. public enum LogFile
  363. {
  364. Trace,
  365. Error,
  366. SQL,
  367. SQLError,
  368. Login,
  369. Info,
  370. WeChat
  371. }
  372. }