LogHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using log4net;
  9. using log4net.Config;
  10. namespace StressClient.common
  11. {
  12. public static class LogHelper
  13. {
  14. //private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod()?.DeclaringType);
  15. //static LogHelper()
  16. //{
  17. // XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
  18. // // var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
  19. // //Log = LogManager.GetLogger(typeof(LogHelper));
  20. //}
  21. /// <summary>
  22. /// 记录调试信息
  23. /// </summary>
  24. /// <param name="ex">信息</param>
  25. public static void Debug(this Object obj, object message)
  26. {
  27. ILog Log = LogManager.GetLogger(obj.GetType());
  28. Log.Debug(message);
  29. }
  30. /// <summary>
  31. /// 记录警告信息
  32. /// </summary>
  33. /// <param name="ex">信息</param>
  34. public static void Warn(this Object obj, object message)
  35. {
  36. ILog Log = LogManager.GetLogger(obj.GetType());
  37. Log.Warn(message);
  38. }
  39. /// <summary>
  40. /// 记录错误信息
  41. /// </summary>
  42. /// <param name="ex">信息</param>
  43. public static void Error(this Object obj, object message)
  44. {
  45. ILog Log = LogManager.GetLogger(obj.GetType());
  46. Log.Error(message);
  47. }
  48. /// <summary>
  49. /// 记录重要提示信息
  50. /// </summary>
  51. /// <param name="ex">信息</param>
  52. public static void Info(this Object obj,object message)
  53. {
  54. ILog Log = LogManager.GetLogger(obj.GetType());
  55. Log.Info(message);
  56. }
  57. /// <summary>
  58. /// 记录信息和异常信息
  59. /// </summary>
  60. /// <param name="message">错误信息</param>
  61. /// <param name="ex">异常对象</param>
  62. public static void Debug(this Object obj,object message, Exception ex)
  63. {
  64. ILog Log = LogManager.GetLogger(obj.GetType());
  65. Log.Debug(message, ex);
  66. }
  67. /// <summary>
  68. /// 记录信息和异常信息
  69. /// </summary>
  70. /// <param name="message">错误信息</param>
  71. /// <param name="ex">异常对象</param>
  72. public static void Warn(this Object obj, object message, Exception ex)
  73. {
  74. ILog Log = LogManager.GetLogger(obj.GetType());
  75. Log.Warn(message, ex);
  76. }
  77. /// <summary>
  78. /// 记录信息和异常信息
  79. /// </summary>
  80. /// <param name="message">错误信息</param>
  81. /// <param name="ex">异常对象</param>
  82. public static void Error(this Object obj, object message, Exception ex)
  83. {
  84. ILog Log = LogManager.GetLogger(obj.GetType());
  85. Log.Error(message, ex);
  86. }
  87. /// <summary>
  88. /// 记录信息和异常信息
  89. /// </summary>
  90. /// <param name="message">错误信息</param>
  91. /// <param name="ex">异常对象</param>
  92. public static void Info(this Object obj, object message, Exception ex)
  93. {
  94. ILog Log = LogManager.GetLogger(obj.GetType());
  95. Log.Info(message, ex);
  96. }
  97. }
  98. }