LogHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using log4net;
  7. using System.IO;
  8. namespace Logs
  9. {
  10. public class LogHelper
  11. {
  12. static LogHelper()
  13. {
  14. string path = System.AppDomain.CurrentDomain.BaseDirectory + @"\logconfig.xml";
  15. log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
  16. }
  17. /// <summary>
  18. /// 接口
  19. /// </summary>
  20. private static ILog __GetLog(Object obj)
  21. {
  22. string name = string.Empty;
  23. if (obj.GetType().Name == "String")
  24. {
  25. name = obj.ToString();
  26. }
  27. else
  28. {
  29. name = obj.GetType().Name;
  30. }
  31. return log4net.LogManager.GetLogger(name);
  32. }
  33. /// <summary>
  34. /// 调试
  35. /// </summary>
  36. /// <param name=”message”>消息</param>
  37. public static void Debug(string message)
  38. {
  39. ILog log = __GetLog(string.Empty);
  40. if (log.IsDebugEnabled)
  41. log.Debug(message);
  42. }
  43. public static void Debug(Object obj, string message)
  44. {
  45. ILog log = __GetLog(obj);
  46. if (log.IsDebugEnabled)
  47. __GetLog(obj).Debug(message);
  48. }
  49. /// <summary>
  50. /// 调试
  51. /// </summary>
  52. /// <param name=”message”>消息</param>
  53. /// <param name=”exception”>异常</param>
  54. public static void Debug(string message, Exception ex)
  55. {
  56. ILog log = __GetLog(string.Empty);
  57. if (log.IsDebugEnabled)
  58. log.Debug(message, ex);
  59. }
  60. public static void Debug(Object obj, string message, Exception ex)
  61. {
  62. ILog log = __GetLog(obj);
  63. if (log.IsDebugEnabled)
  64. __GetLog(obj).Debug(message, ex);
  65. }
  66. /// <summary>
  67. /// 信息
  68. /// </summary>
  69. /// <param name=”message”>消息</param>
  70. public static void Info(string message)
  71. {
  72. ILog log = __GetLog(string.Empty);
  73. if (log.IsInfoEnabled)
  74. log.Info(message);
  75. }
  76. /// <summary>
  77. /// 信息
  78. /// </summary>
  79. /// <param name=”message”>消息</param>
  80. /// <param name=”exception”>异常</param>
  81. public static void Info(string message, Exception ex)
  82. {
  83. ILog log = __GetLog(string.Empty);
  84. if (log.IsInfoEnabled)
  85. log.Info(message, ex);
  86. }
  87. /// <summary>
  88. /// 一般错误
  89. /// </summary>
  90. /// <param name=”message”>消息</param>
  91. public static void Error(string message)
  92. {
  93. ILog log = __GetLog(string.Empty);
  94. if (log.IsErrorEnabled)
  95. log.Error(message);
  96. }
  97. /// <summary>
  98. /// 一般错误
  99. /// </summary>
  100. /// <param name=”message”>消息</param>
  101. /// <param name=”exception”>异常</param>
  102. public static void Error(string message, Exception exception)
  103. {
  104. ILog log = __GetLog(string.Empty);
  105. if (log.IsErrorEnabled)
  106. log.Error(message, exception);
  107. }
  108. /// <summary>
  109. /// 警告
  110. /// </summary>
  111. /// <param name=”message”>消息</param>
  112. public static void Warn(string message)
  113. {
  114. ILog log = __GetLog(string.Empty);
  115. if (log.IsWarnEnabled)
  116. log.Warn(message);
  117. }
  118. /// <summary>
  119. /// 警告
  120. /// </summary>
  121. /// <param name=”message”>消息</param>
  122. /// <param name=”exception”>异常</param>
  123. public static void Warn(string message, Exception ex)
  124. {
  125. ILog log = __GetLog(string.Empty);
  126. if (log.IsWarnEnabled)
  127. log.Warn(message, ex);
  128. }
  129. /// <summary>
  130. /// 严重
  131. /// </summary>
  132. /// <param name=”message”>消息</param>
  133. public static void Fatal(string message)
  134. {
  135. ILog log = __GetLog(string.Empty);
  136. if (log.IsFatalEnabled)
  137. log.Fatal(message);
  138. }
  139. /// <summary>
  140. /// 严重
  141. /// </summary>
  142. /// <param name=”message”>消息</param>
  143. /// <param name=”exception”>异常</param>
  144. public static void Fatal(string message, Exception ex)
  145. {
  146. ILog log = __GetLog(string.Empty);
  147. if (log.IsFatalEnabled)
  148. log.Fatal(message, ex);
  149. }
  150. }
  151. }