LoggerExtension.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SuperSocket.Common;
  6. using SuperSocket.SocketBase.Logging;
  7. namespace SuperSocket.SocketBase
  8. {
  9. /// <summary>
  10. /// Logger extension class
  11. /// </summary>
  12. public static class LoggerExtension
  13. {
  14. private readonly static string m_SessionInfoTemplate = "Session: {0}/{1}";
  15. /// <summary>
  16. /// Logs the error
  17. /// </summary>
  18. /// <param name="logger">The logger.</param>
  19. /// <param name="session">The session.</param>
  20. /// <param name="title">The title.</param>
  21. /// <param name="e">The e.</param>
  22. public static void Error(this ILog logger, ISessionBase session, string title, Exception e)
  23. {
  24. logger.Error(string.Format(m_SessionInfoTemplate, session.SessionID, session.RemoteEndPoint) + Environment.NewLine + title, e);
  25. }
  26. /// <summary>
  27. /// Logs the error
  28. /// </summary>
  29. /// <param name="logger">The logger.</param>
  30. /// <param name="session">The session.</param>
  31. /// <param name="message">The message.</param>
  32. public static void Error(this ILog logger, ISessionBase session, string message)
  33. {
  34. logger.Error(string.Format(m_SessionInfoTemplate, session.SessionID, session.RemoteEndPoint) + Environment.NewLine + message);
  35. }
  36. /// <summary>
  37. /// Logs the information
  38. /// </summary>
  39. /// <param name="logger">The logger.</param>
  40. /// <param name="session">The session.</param>
  41. /// <param name="message">The message.</param>
  42. public static void Info(this ILog logger, ISessionBase session, string message)
  43. {
  44. string info = string.Format(m_SessionInfoTemplate, session.SessionID, session.RemoteEndPoint) + Environment.NewLine + message;
  45. logger.Info(info);
  46. }
  47. /// <summary>
  48. /// Logs the debug message
  49. /// </summary>
  50. /// <param name="logger">The logger.</param>
  51. /// <param name="session">The session.</param>
  52. /// <param name="message">The message.</param>
  53. public static void Debug(this ILog logger, ISessionBase session, string message)
  54. {
  55. if (!logger.IsDebugEnabled)
  56. return;
  57. logger.Debug(string.Format(m_SessionInfoTemplate, session.SessionID, session.RemoteEndPoint) + Environment.NewLine + message);
  58. }
  59. private const string m_PerfLogName = "Perf";
  60. private static ILog m_PerfLog;
  61. /// <summary>
  62. /// Logs the performance message
  63. /// </summary>
  64. /// <param name="appServer">The app server.</param>
  65. /// <param name="message">The message.</param>
  66. public static void LogPerf(this IAppServer appServer, string message)
  67. {
  68. if (m_PerfLog == null)
  69. {
  70. lock (m_PerfLogName)
  71. {
  72. if (m_PerfLog == null)
  73. {
  74. m_PerfLog = appServer.LogFactory.GetLog(m_PerfLogName);
  75. }
  76. }
  77. }
  78. if (m_PerfLog != null && m_PerfLog.IsInfoEnabled)
  79. m_PerfLog.Info(message);
  80. }
  81. }
  82. }