課程地址:
https://study.163.com/course/courseMain.htm?courseId=1004873017
本課項目地址:
https://github.com/wechatdeveloper/WechatVideoCourse
課程目標:
熟悉微信接口請求中的日志記錄方法,SDK在請求微信接口的時候,自動記錄了日志信息。
從而方便快速的定位線上異常,WeixinTrace.Log() 方法是最終觸發記錄日志的地方。
一、微信接口日志
日志開啟方式,Global.asax 中開啟:
//設置全局 Debug 狀態 var isGLobalDebug = true;
App_Data文件夾中,生成了日志文件:
日志內容的格式:
二:日志觸發事件
日志觸發的回調事件:
1 //自定義日志記錄回調 2 Senparc.Weixin.WeixinTrace.OnLogFunc = () => 3 { 4 //加入每次觸發Log后需要執行的代碼 ,如日志記錄的次數 5 ConfigData.LogRecordCount++; 6 };
WeixinException的異常時觸發
1 //當發生基於WeixinException的異常時觸發 2 Senparc.Weixin.WeixinTrace.OnWeixinExceptionFunc = ex => 3 { 4 //加入每次觸發WeixinExceptionLog后需要執行的代碼 5 ConfigData.LogExceptionRecordCount++; 6 };
三:異常類型
SDK 對經常會出現的異常,定義了類,異常會到對應的類中,進行日志的記錄
https://github.com/JeffreySu/WeiXinMPSDK/tree/master/src/Senparc.Weixin/Senparc.Weixin/Exceptions
四、日志管理、查看插件
插件源碼位置:https://github.com/JeffreySu/WeiXinMPSDK/tree/master/Plugins
方便查看、檢索生成的日志文件效果:
把項目日志文件Copy到App_Data文件夾下面: