Log4NetHelper.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using log4net;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Web;
  8. namespace Central.Control.WebApi.Log4net
  9. {
  10. /// <summary>
  11. /// 日志工具类
  12. /// </summary>
  13. public class Log4NetHelper: ILog4NetHelper
  14. {
  15. public Log4NetHelper()
  16. {
  17. log4net.Config.XmlConfigurator.Configure(new FileInfo(HttpContext.Current.Server.MapPath("~/App_Data/Config/log4net.config")));
  18. }
  19. public void Debug(object message)
  20. {
  21. LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message);
  22. }
  23. public void Debug(object message, Exception ex)
  24. {
  25. LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex);
  26. }
  27. public void Error(object message)
  28. {
  29. LogManager.GetLogger(GetCurrentMethodFullName()).Error(message);
  30. }
  31. public void Error(object message, Exception exception)
  32. {
  33. LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception);
  34. }
  35. private string GetCurrentMethodFullName()
  36. {
  37. try
  38. {
  39. StackFrame frame;
  40. string str2;
  41. int num = 2;
  42. StackTrace trace = new StackTrace();
  43. int length = trace.GetFrames().Length;
  44. do
  45. {
  46. frame = trace.GetFrame(num++);
  47. str2 = frame.GetMethod().DeclaringType.ToString();
  48. }
  49. while (str2.EndsWith("Exception") && (num < length));
  50. string name = frame.GetMethod().Name;
  51. return (str2 + "." + name);
  52. }
  53. catch
  54. {
  55. return null;
  56. }
  57. }
  58. public void Info(object message)
  59. {
  60. LogManager.GetLogger(GetCurrentMethodFullName()).Info(message);
  61. }
  62. public void Info(object message, Exception ex)
  63. {
  64. LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex);
  65. }
  66. public void Warn(object message)
  67. {
  68. LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message);
  69. }
  70. public void Warn(object message, Exception ex)
  71. {
  72. LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message, ex);
  73. }
  74. }
  75. }