You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.0 KiB

2 years ago
  1. ///-----------------------------------------------------------------------
  2. /// <copyright file="LogHelper.cs" company="Eurotran">
  3. /// 程式代號: LogHelper
  4. /// 程式名稱: LogHelper
  5. /// 程式說明:
  6. /// 起始作者:
  7. /// 起始日期: 2017/05/09 16:14:59
  8. /// 最新修改人:
  9. /// 最新修日期: 2017/05/18 17:45:54
  10. /// </copyright>
  11. ///-----------------------------------------------------------------------
  12. namespace EasyBL
  13. {
  14. using log4net;
  15. using log4net.Appender;
  16. using log4net.Repository;
  17. using log4net.Repository.Hierarchy;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Xml;
  24. /// <summary>
  25. /// 類別名稱:LogHelper
  26. /// 類別說明:
  27. /// 起始作者:
  28. /// 起始日期:
  29. /// 最新修改人:
  30. /// 最新修改日:
  31. /// </summary>
  32. public static class LogHelper
  33. {
  34. /// <summary>
  35. /// 函式名稱:Flush
  36. /// 函式說明:
  37. /// 起始作者:
  38. /// 起始日期:
  39. /// 最新修改人:
  40. /// 最新修改日:
  41. /// </summary>
  42. /// <param name="i_iLog">
  43. /// 參數說明
  44. /// </param>
  45. /// <returns>
  46. /// 回傳
  47. /// </returns>
  48. public static void Flush(ILog i_iLog)
  49. {
  50. //ILoggerRepository rep = LogManager.GetRepository();
  51. //foreach (IAppender appender in rep.GetAppenders())
  52. //{
  53. // var buffered = appender as BufferingAppenderSkeleton;
  54. // if (buffered != null)
  55. // {
  56. // buffered.Flush();
  57. // }
  58. //}
  59. var logger = i_iLog.Logger as Logger;
  60. if (logger != null)
  61. {
  62. foreach (IAppender appender in logger.Appenders)
  63. {
  64. var buffered = appender as BufferingAppenderSkeleton;
  65. if (buffered != null)
  66. {
  67. buffered.Flush();
  68. }
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// 函式名稱:MakeDirs
  74. /// 函式說明:
  75. /// 起始作者:
  76. /// 起始日期:
  77. /// 最新修改人:
  78. /// 最新修改日:
  79. /// </summary>
  80. /// <param name="i_sRootPath"></param>
  81. /// <param name="i_sPath">
  82. /// 參數說明
  83. /// </param>
  84. /// <returns>
  85. /// 回傳
  86. /// </returns>
  87. private static void MakeDirs(string i_sRootPath, string i_sPath)
  88. {
  89. XmlDocument xmlDoc = new XmlDocument();
  90. try
  91. {
  92. xmlDoc.Load(i_sPath);
  93. XmlNodeList nodeList = xmlDoc.SelectNodes("/configuration/log4net/appender/file");
  94. foreach (XmlNode folderPath in nodeList)
  95. {
  96. string sPath = System.IO.Path.Combine(i_sRootPath, folderPath.Attributes["value"].Value.ToString());
  97. if (Directory.Exists(sPath) == false)
  98. {
  99. Directory.CreateDirectory(sPath);
  100. }
  101. }
  102. }
  103. catch
  104. {
  105. }
  106. }
  107. /// <summary>
  108. /// 函式名稱:Init
  109. /// 函式說明:
  110. /// 起始作者:
  111. /// 起始日期:
  112. /// 最新修改人:
  113. /// 最新修改日:
  114. /// </summary>
  115. /// <param name="i_sWorkSpace"></param>
  116. /// <param name="i_sConfigPath">
  117. /// 參數說明
  118. /// </param>
  119. /// <returns>
  120. /// 回傳
  121. /// </returns>
  122. public static void Init(string i_sWorkSpace, string i_sConfigPath)
  123. {
  124. MakeDirs(i_sWorkSpace, i_sConfigPath);
  125. //string sConfigContent = File.ReadAllText(i_sConfigPath, Encoding.UTF8);
  126. //MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(sConfigContent));
  127. //log4net.Config.XmlConfigurator.Configure(mStrm);
  128. }
  129. }
  130. }