SocketSession.Net45.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using SuperSocket.SocketBase;
  8. namespace SuperSocket.SocketEngine
  9. {
  10. abstract partial class SocketSession
  11. {
  12. private const string m_GeneralErrorMessage = "Unexpected error";
  13. private const string m_GeneralSocketErrorMessage = "Unexpected socket error: {0}";
  14. private const string m_CallerInformation = "Caller: {0}, file path: {1}, line number: {2}";
  15. /// <summary>
  16. /// Logs the error, skip the ignored exception
  17. /// </summary>
  18. /// <param name="exception">The exception.</param>
  19. /// <param name="caller">The caller.</param>
  20. /// <param name="callerFilePath">The caller file path.</param>
  21. /// <param name="callerLineNumber">The caller line number.</param>
  22. protected void LogError(Exception exception, [CallerMemberName] string caller = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
  23. {
  24. int socketErrorCode;
  25. //This exception is ignored, needn't log it
  26. if (IsIgnorableException(exception, out socketErrorCode))
  27. return;
  28. var message = socketErrorCode > 0 ? string.Format(m_GeneralSocketErrorMessage, socketErrorCode) : m_GeneralErrorMessage;
  29. AppSession.Logger.Error(this
  30. , message + Environment.NewLine + string.Format(m_CallerInformation, caller, callerFilePath, callerLineNumber)
  31. , exception);
  32. }
  33. /// <summary>
  34. /// Logs the error, skip the ignored exception
  35. /// </summary>
  36. /// <param name="message">The message.</param>
  37. /// <param name="exception">The exception.</param>
  38. /// <param name="caller">The caller.</param>
  39. /// <param name="callerFilePath">The caller file path.</param>
  40. /// <param name="callerLineNumber">The caller line number.</param>
  41. protected void LogError(string message, Exception exception, [CallerMemberName] string caller = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
  42. {
  43. int socketErrorCode;
  44. //This exception is ignored, needn't log it
  45. if (IsIgnorableException(exception, out socketErrorCode))
  46. return;
  47. AppSession.Logger.Error(this
  48. , message + Environment.NewLine + string.Format(m_CallerInformation, caller, callerFilePath, callerLineNumber)
  49. , exception);
  50. }
  51. /// <summary>
  52. /// Logs the socket error, skip the ignored error
  53. /// </summary>
  54. /// <param name="socketErrorCode">The socket error code.</param>
  55. /// <param name="caller">The caller.</param>
  56. /// <param name="callerFilePath">The caller file path.</param>
  57. /// <param name="callerLineNumber">The caller line number.</param>
  58. protected void LogError(int socketErrorCode, [CallerMemberName] string caller = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
  59. {
  60. if (!Config.LogAllSocketException)
  61. {
  62. //This error is ignored, needn't log it
  63. if (IsIgnorableSocketError(socketErrorCode))
  64. return;
  65. }
  66. AppSession.Logger.Error(this
  67. , string.Format(m_GeneralSocketErrorMessage, socketErrorCode) + Environment.NewLine + string.Format(m_CallerInformation, caller, callerFilePath, callerLineNumber)
  68. , new SocketException(socketErrorCode));
  69. }
  70. }
  71. }