终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

470 строки
16 KiB

  1. using log4net;
  2. using log4net.Repository;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BPASmartClient.Helper
  10. {
  11. /// <summary>
  12. /// 创建人:奉友福
  13. /// 时间:2021/11/12
  14. /// </summary>
  15. public class logHelper
  16. {
  17. #region 单例模式
  18. static private logHelper m_logHelperInstance = null;
  19. public static logHelper GetLogConfigInstance()
  20. {
  21. if (null == m_logHelperInstance)
  22. {
  23. m_logHelperInstance = new logHelper();
  24. }
  25. return m_logHelperInstance;
  26. }
  27. #endregion
  28. /// <summary>
  29. /// 日志显示委托
  30. /// </summary>
  31. public Action<string> InfoNotify { get; set; }
  32. public ILog directLogger = null;
  33. public log4net.Appender.RollingFileAppender directRollfileAppender = null;
  34. ILoggerRepository logRepos = null;
  35. private static bool m_isInitDirectLog = false; //是否初始化了直接写入日志文件
  36. private static Int32 directFileSize = 50;
  37. private static string directLogDir = string.Empty;
  38. private static string directLogFileName = string.Empty;
  39. //static private UAFLogHelper uafLogInstance = null;
  40. private LogLevel logLevel = LogLevel.ALL;
  41. /// <summary>
  42. /// 初始化日志
  43. /// </summary>
  44. /// <param name="path"></param>
  45. public static void Fun_InitLog(string path)
  46. {
  47. try
  48. {
  49. /// 设置需要的日志信息
  50. GetLogConfigInstance().SetConfigEnableThreadId(true);
  51. GetLogConfigInstance().SetConfigEnableAppDomain(true);
  52. /// 使用直接日志接口写入日志信息
  53. GetLogConfigInstance().SetLogLevel(LogLevel.ERROR);
  54. GetLogConfigInstance().SetDirectFileLogInfo(5, string.Empty);
  55. GetLogConfigInstance().SetDirectFileLogName("HBL.LogDir");
  56. if (!(GetLogConfigInstance().initDirectLogger(path)))
  57. {
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. }
  63. }
  64. public void SetDirectFileLogInfo(Int32 fileSize, string logDirectory)
  65. {
  66. directFileSize = fileSize;
  67. directLogDir = logDirectory;
  68. }
  69. public void SetDirectFileLogName(string logFileName)
  70. {
  71. directLogFileName = logFileName;
  72. }
  73. //初始化日志实例
  74. public bool initDirectLogger(string loggerName)
  75. {
  76. try
  77. {
  78. logRepos = log4net.LogManager.CreateRepository(loggerName);
  79. //初始化记录日志到文件的 appender
  80. InitDirectRollFileAppender(loggerName);
  81. m_isInitDirectLog = true;
  82. return true;
  83. }
  84. catch (Exception e)
  85. {
  86. throw e;
  87. }
  88. }
  89. private void InitDirectRollFileAppender(string loggerName)
  90. {
  91. try
  92. {
  93. directLogger = LogManager.GetLogger(logRepos.Name, loggerName);
  94. //初始化记录日志到文件的 appender
  95. directRollfileAppender = new log4net.Appender.RollingFileAppender();
  96. // pattern layout
  97. log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout();
  98. //UAFCustomDataDB.UAFCustomLayout layout = new UAFCustomDataDB.UAFCustomLayout();
  99. //layout.ConversionPattern = "%log_time %log_threadname %log_ip %-5level %log_domain %log_location %log_msg %log_details%n"; // pattern
  100. layout.ConversionPattern = string.Empty;
  101. if (LogConfig.GetLogConfigInstance().EnableDate)
  102. {
  103. layout.ConversionPattern += "%date{yyyy-MM-dd HH:mm:ss} ";
  104. }
  105. //级别
  106. layout.ConversionPattern += "%-5level ";
  107. if (LogConfig.GetLogConfigInstance().EnableAppDomain)
  108. {
  109. layout.ConversionPattern += "%a ";
  110. }
  111. if (LogConfig.GetLogConfigInstance().EnableThreadId)
  112. {
  113. layout.ConversionPattern += "[%t] ";
  114. }
  115. layout.ConversionPattern += "%m%n";
  116. layout.ActivateOptions(); // activate
  117. //directRollfileAppender.CloseFlag = false; //添加打开标志
  118. directRollfileAppender.Layout = layout; // layout pattern
  119. //directRollfileAppender.Name = "DirectLogRollFileAppender";
  120. directRollfileAppender.Name = loggerName;
  121. directRollfileAppender.File = string.Empty;
  122. //设置日志文件路径
  123. if (string.Empty == directLogDir)
  124. {
  125. directRollfileAppender.File = "LogDir\\";
  126. }
  127. else
  128. {
  129. if (directLogDir.EndsWith("\\"))
  130. {
  131. directRollfileAppender.File = directLogDir;
  132. }
  133. else
  134. {
  135. directRollfileAppender.File = directLogDir + "\\";
  136. }
  137. }
  138. //设置日志文件名字
  139. if (string.Empty == directLogFileName)
  140. {
  141. //获取进程Id,构造日志文件名字
  142. string loggerFileName = Process.GetCurrentProcess().ProcessName
  143. + Process.GetCurrentProcess().Id.ToString() + "_"
  144. + DateTime.Now.Year.ToString()
  145. + DateTime.Now.Month.ToString()
  146. + DateTime.Now.Day.ToString()
  147. + DateTime.Now.Hour.ToString()
  148. + DateTime.Now.Minute.ToString()
  149. + DateTime.Now.Second.ToString()
  150. + DateTime.Now.Millisecond.ToString() + "_";//"UAFMsgName";
  151. directRollfileAppender.File += loggerFileName;
  152. }
  153. else
  154. {
  155. directRollfileAppender.File += directLogFileName;
  156. }
  157. directRollfileAppender.AppendToFile = true; // overwrite any existin g file with the same name
  158. if (directFileSize <= 0)
  159. {
  160. directRollfileAppender.MaxFileSize = (50 * 1024 * 1024); // max file size - 50*1024*1024 = 50MB
  161. }
  162. else
  163. {
  164. directRollfileAppender.MaxFileSize = (directFileSize * 1024 * 1024);
  165. }
  166. directRollfileAppender.MaxSizeRollBackups = 50; // keep 50 files
  167. directRollfileAppender.ImmediateFlush = true; // flush after every append
  168. directRollfileAppender.Threshold = log4net.Core.Level.All;
  169. directRollfileAppender.DatePattern = "yyyy_M_d'.log'";
  170. directRollfileAppender.StaticLogFileName = false;
  171. directRollfileAppender.ActivateOptions(); // activate
  172. // add appenders
  173. //log4net.Config.BasicConfigurator.Configure(directRollfileAppender);
  174. log4net.Config.BasicConfigurator.Configure(logRepos, directRollfileAppender);
  175. }
  176. catch (Exception e)
  177. {
  178. throw e;
  179. }
  180. }
  181. //重新设置日志保存路径和文件名
  182. public void ReconfigLogDirAndName()
  183. {
  184. directRollfileAppender.File = string.Empty;
  185. //设置日志文件路径
  186. if (string.Empty == directLogDir)
  187. {
  188. directRollfileAppender.File = "LogDir\\";
  189. }
  190. else
  191. {
  192. if (directLogDir.EndsWith("\\"))
  193. {
  194. directRollfileAppender.File = directLogDir;
  195. }
  196. else
  197. {
  198. directRollfileAppender.File = directLogDir + "\\";
  199. }
  200. }
  201. //设置日志文件名字
  202. if (string.Empty == directLogFileName)
  203. {
  204. //获取进程Id,构造日志文件名字
  205. string loggerFileName = Process.GetCurrentProcess().ProcessName
  206. + Process.GetCurrentProcess().Id.ToString() + "_"
  207. + DateTime.Now.Year.ToString()
  208. + DateTime.Now.Month.ToString()
  209. + DateTime.Now.Day.ToString()
  210. + DateTime.Now.Hour.ToString()
  211. + DateTime.Now.Minute.ToString()
  212. + DateTime.Now.Second.ToString()
  213. + DateTime.Now.Millisecond.ToString() + "_";//"UAFMsgName";
  214. directRollfileAppender.File += loggerFileName;
  215. }
  216. else
  217. {
  218. directRollfileAppender.File += directLogFileName;
  219. }
  220. if (directFileSize <= 0)
  221. {
  222. directRollfileAppender.MaxFileSize = (50 * 1024 * 1024); // max file size - 50*1024*1024 = 50MB
  223. }
  224. else
  225. {
  226. directRollfileAppender.MaxFileSize = (directFileSize * 1024 * 1024);
  227. }
  228. directRollfileAppender.ActivateOptions(); // activate
  229. }
  230. public void SetLogLevel(LogLevel level)
  231. {
  232. logLevel = level;
  233. }
  234. public bool WriteLog(LogLevel level, string logMsg)
  235. {
  236. try
  237. {
  238. if (null == directLogger)
  239. {
  240. return false;
  241. }
  242. //if ((LogLevel.OFF == logLevel) || ((Int32)level > (Int32)logLevel /*&& LogObjectLib.LogLevel.ALL != logLevel*/))
  243. //{
  244. // return true;
  245. //}
  246. //调用日志接口直接写日志信息
  247. switch (level)
  248. {
  249. case LogLevel.FATAL:
  250. if (directLogger.IsFatalEnabled)
  251. {
  252. directLogger.Fatal(logMsg);
  253. }
  254. break;
  255. case LogLevel.ERROR:
  256. if (directLogger.IsErrorEnabled)
  257. {
  258. directLogger.Error(logMsg);
  259. }
  260. break;
  261. case LogLevel.WARN:
  262. if (directLogger.IsWarnEnabled)
  263. {
  264. directLogger.Warn(logMsg);
  265. }
  266. break;
  267. case LogLevel.INFO:
  268. if (directLogger.IsInfoEnabled)
  269. {
  270. directLogger.Info(logMsg);
  271. }
  272. break;
  273. case LogLevel.DEBUG:
  274. if (directLogger.IsDebugEnabled)
  275. {
  276. directLogger.Debug(logMsg);
  277. }
  278. break;
  279. default:
  280. break;
  281. }
  282. if (InfoNotify != null)
  283. {
  284. logMsg = $"{DateTime.Now.ToString("HH:mm:ss")}:{level} {logMsg} \n\r";
  285. InfoNotify(logMsg);
  286. }
  287. return true;
  288. }
  289. catch (Exception e)
  290. {
  291. throw e;
  292. }
  293. }
  294. //设置日志配置--app域名
  295. public void SetConfigEnableAppDomain(bool value)
  296. {
  297. LogConfig.GetLogConfigInstance().EnableAppDomain = value;
  298. }
  299. //设置日志配置--线程id
  300. public void SetConfigEnableThreadId(bool value)
  301. {
  302. LogConfig.GetLogConfigInstance().EnableThreadId = value;
  303. }
  304. public bool GetConfigEnableAppDomain()
  305. {
  306. return LogConfig.GetLogConfigInstance().EnableAppDomain;
  307. }
  308. public bool GetConfigEnableThreadId()
  309. {
  310. return LogConfig.GetLogConfigInstance().EnableThreadId;
  311. }
  312. /// <summary>
  313. /// 打开指定路径下文件
  314. /// </summary>
  315. public void OpenFile(string NewFileName)
  316. {
  317. Process process = new Process();
  318. ProcessStartInfo processStartInfo = new ProcessStartInfo(NewFileName);
  319. process.StartInfo = processStartInfo;
  320. #region 下面这段被注释掉代码(可以用来全屏打开代码)
  321. //建立新的系统进程
  322. ////System.Diagnostics.Process process = new System.Diagnostics.Process();
  323. //设置文件名,此处为图片的真实路径 + 文件名(需要有后缀)
  324. ////process.StartInfo.FileName = NewFileName;
  325. //此为关键部分。设置进程运行参数,此时为最大化窗口显示图片。
  326. ////process.StartInfo.Arguments = "rundll32.exe C://WINDOWS//system32//shimgvw.dll,ImageView_Fullscreen";
  327. // 此项为是否使用Shell执行程序,因系统默认为true,此项也可不设,但若设置必须为true
  328. process.StartInfo.UseShellExecute = true;
  329. #endregion
  330. try
  331. {
  332. process.Start();
  333. try
  334. {
  335. // process.WaitForExit();
  336. }
  337. catch (Exception ex)
  338. {
  339. throw ex;
  340. }
  341. }
  342. catch (Exception ex)
  343. {
  344. throw ex;
  345. }
  346. finally
  347. {
  348. try
  349. {
  350. if (process != null)
  351. {
  352. process.Close();
  353. process = null;
  354. }
  355. }
  356. catch { }
  357. }
  358. }
  359. }
  360. /// <summary>
  361. /// 创建人:奉友福
  362. /// </summary>
  363. public class LogConfig
  364. {
  365. private bool m_enableDate = true;
  366. private bool m_enableAppDomain = false;
  367. private bool m_enableThreadId = false;
  368. #region 单例模式
  369. static private LogConfig m_logConfigInstance = null;
  370. public static LogConfig GetLogConfigInstance()
  371. {
  372. if (null == m_logConfigInstance)
  373. {
  374. m_logConfigInstance = new LogConfig();
  375. }
  376. return m_logConfigInstance;
  377. }
  378. #endregion
  379. private LogConfig()
  380. {
  381. }
  382. public bool EnableDate
  383. {
  384. set
  385. {
  386. m_enableDate = value;
  387. }
  388. get { return m_enableDate; }
  389. }
  390. public bool EnableAppDomain
  391. {
  392. set
  393. {
  394. m_enableAppDomain = value;
  395. }
  396. get { return m_enableAppDomain; }
  397. }
  398. public bool EnableThreadId
  399. {
  400. set
  401. {
  402. m_enableThreadId = value;
  403. }
  404. get { return m_enableThreadId; }
  405. }
  406. }
  407. /// <summary>
  408. /// 创建人:奉友福
  409. /// 时间:2021/11/12
  410. /// </summary>
  411. public enum LogLevel
  412. {
  413. OFF = 0,
  414. FATAL,
  415. ERROR,
  416. WARN,
  417. INFO,
  418. DEBUG,
  419. ALL
  420. }
  421. }