閱讀271 返回首頁    go 群英


unity3d__loghub-采集_用戶指南_日誌服務-阿裏雲

Unity3D是由Unity Technologies開發的一個讓玩家輕鬆創建諸如三維視頻遊戲、建築可視化、實時三維動畫等類型互動內容的多平台的綜合型遊戲開發工具,是一個全麵整合的專業遊戲引擎。日誌服務前不久推出了Web Tracking功能,您可以通過Web Tracking功能非常方便的收集Unity 3D的日誌,下麵以收集Unity Debug.Log為例,講解如何將Unity日誌收集到日誌服務中。

step 1: 開通Web Tracking功能

開通方法請參考另外一篇文章:日誌服務Tracking功能

step 2: 注冊Unity3D LogHandler

在Unity editor中創建c#文件LogOutputHandler.cs,並將下麵的代碼拷貝進去,修改其中的三個成員變量,分別是日誌項目的名稱project,日誌庫的名字logstore,日誌項目的地址serviceAddr,serviceAddr可以從日誌服務官方文檔中找到。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class LogOutputHandler : MonoBehaviour
  4. {
  5. //Register the HandleLog function on scene start to fire on debug.log events
  6. public void OnEnable()
  7. {
  8. Application.logMessageReceived += HandleLog;
  9. }
  10. //Remove callback when object goes out of scope
  11. public void OnDisable()
  12. {
  13. Application.logMessageReceived -= HandleLog;
  14. }
  15. string project = "your project name";
  16. string logstore = "your logstore name";
  17. string serviceAddr = "http address of your log service project";
  18. //Capture debug.log output, send logs to Loggly
  19. public void HandleLog(string logString, string stackTrace, LogType type)
  20. {
  21. string parameters = "";
  22. parameters += "Level=" + WWW.EscapeURL(type.ToString());
  23. parameters += "&";
  24. parameters += "Message=" + WWW.EscapeURL(logString);
  25. parameters += "&";
  26. parameters += "Stack_Trace=" + WWW.EscapeURL(stackTrace);
  27. parameters += "&";
  28. //Add any User, Game, or Device MetaData that would be useful to finding issues later
  29. parameters += "Device_Model=" + WWW.EscapeURL(SystemInfo.deviceModel);
  30. string url = "https://" + project + "." + serviceAddr + "/logstores/" + logstore + "/track?APIVersion=0.6.0&" + parameters;
  31. StartCoroutine(SendData(url));
  32. }
  33. public IEnumerator SendData(string url)
  34. {
  35. WWW sendLog = new WWW(url);
  36. yield return sendLog;
  37. }
  38. }

上麵的代碼可以異步的將日誌發送到阿裏雲日誌服務中,在示例中您可以添加更多想要收集的字段。

step 3:產生Unity日誌。

在工程中創建LogglyTest.cs文件,並加入下麵的代碼:

  1. using UnityEngine;
  2. using System.Collections;
  3. public class LogglyTest : MonoBehaviour {
  4. void Start () {
  5. Debug.Log ("Hello world");
  6. }
  7. }

step 4: 到日誌服務控製台查看。

上述步驟做完之後,運行Unity程序,就可以在日誌服務的控製台看到您發送的日誌了。

總結

上麵的例子中給出了Debug.Log或者類似的比如Debug.LogError、Debug.LogException日誌的收集方法,Unity的組件對象模型以及其提供的程序崩潰API、其他各種LOG API使得可以非常方便的收集客戶端的設備信息,這些我將會在接下來的文章中介紹。

最後更新:2016-09-21 13:52:02

  上一篇:go syslog__loghub-采集_用戶指南_日誌服務-阿裏雲
  下一篇:go 消費日誌__loghub-消費_用戶指南_日誌服務-阿裏雲