WeixinTrace.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:WeixinTrace.cs
  17. 文件功能描述:跟踪日志相关
  18. 创建标识:Senparc - 20151012
  19. 修改标识:Senparc - 20161225
  20. 修改描述:v4.9.7 1、使用同步锁
  21. 2、修改日志储存路径,新路径为/App_Data/WeixinTraceLog/SenparcWeixinTrace-yyyyMMdd.log
  22. 3、添加WeixinExceptionLog方法
  23. 修改标识:Senparc - 20161231
  24. 修改描述:v4.9.8 将SendLog方法改名为SendApiLog,添加SendCustomLog方法
  25. 修改标识:Senparc - 20170101
  26. 修改描述:v4.9.9 1、优化日志记录方法(围绕OnWeixinExceptionFunc为主)
  27. 2、输出AccessTokenOrAppId
  28. 修改标识:Senparc - 20170304
  29. 修改描述:Senparc.Wexin v4.11.3 日志中添加对线程的记录
  30. 修改标识:Senparc - 20181118
  31. 修改描述:v16.5.0 使用 CO2NET v0.3.0 新的 SenparcTrace 记录方法
  32. ----------------------------------------------------------------*/
  33. using System;
  34. using System.Diagnostics;
  35. using System.IO;
  36. using Senparc.Weixin.Cache;
  37. using Senparc.Weixin.Exceptions;
  38. using System.Threading;
  39. using Senparc.CO2NET.Trace;
  40. using Senparc.CO2NET.Extensions;
  41. namespace Senparc.Weixin
  42. {
  43. //TODO:将WeixinTrace和SenparcTrace通过某种标记明显区分开来
  44. /// <summary>
  45. /// 微信日志跟踪
  46. /// </summary>
  47. public class WeixinTrace : SenparcTrace
  48. {
  49. /// <summary>
  50. /// 记录WeixinException日志时需要执行的任务
  51. /// </summary>
  52. public static Action<WeixinException> OnWeixinExceptionFunc;
  53. /// <summary>
  54. /// 记录系统日志
  55. /// </summary>
  56. /// <param name="messageFormat"></param>
  57. /// <param name="param"></param>
  58. public static void Log(string messageFormat, params object[] param)
  59. {
  60. SenparcTrace.Log(messageFormat.FormatWith(param));
  61. }
  62. #region WeixinException 相关日志
  63. /// <summary>
  64. /// WeixinException 日志
  65. /// </summary>
  66. /// <param name="ex"></param>
  67. public static void WeixinExceptionLog(WeixinException ex)
  68. {
  69. if (!Config.IsDebug)
  70. {
  71. return;
  72. }
  73. using (var traceItem = new SenparcTraceItem(SenparcTrace._logEndActon, "WeixinException"))
  74. {
  75. traceItem.Log(ex.GetType().Name);
  76. traceItem.Log("AccessTokenOrAppId:{0}", ex.AccessTokenOrAppId);
  77. traceItem.Log("Message:{0}", ex.Message);
  78. traceItem.Log("StackTrace:{0}", ex.StackTrace);
  79. if (ex.InnerException != null)
  80. {
  81. traceItem.Log("InnerException:{0}", ex.InnerException.Message);
  82. traceItem.Log("InnerException.StackTrace:{0}", ex.InnerException.StackTrace);
  83. }
  84. }
  85. if (OnWeixinExceptionFunc != null)
  86. {
  87. try
  88. {
  89. OnWeixinExceptionFunc(ex);
  90. }
  91. catch
  92. {
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// ErrorJsonResultException 日志
  98. /// </summary>
  99. /// <param name="ex"></param>
  100. public static void ErrorJsonResultExceptionLog(ErrorJsonResultException ex)
  101. {
  102. if (!Config.IsDebug)
  103. {
  104. return;
  105. }
  106. using (var traceItem = new SenparcTraceItem(SenparcTrace._logEndActon, "ErrorJsonResultException"))
  107. {
  108. traceItem.Log("ErrorJsonResultException");
  109. traceItem.Log("AccessTokenOrAppId:{0}", ex.AccessTokenOrAppId ?? "null");
  110. traceItem.Log("URL:{0}", ex.Url);
  111. traceItem.Log("errcode:{0}", ex.JsonResult.errcode);
  112. traceItem.Log("errmsg:{0}", ex.JsonResult.errmsg);
  113. }
  114. if (OnWeixinExceptionFunc != null)
  115. {
  116. try
  117. {
  118. OnWeixinExceptionFunc(ex);
  119. }
  120. catch
  121. {
  122. }
  123. }
  124. }
  125. #endregion
  126. }
  127. }