HttpsSendLog.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using LitJson;
  2. using SC.XR.Unity;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. public class HttpsSendLog : SingletonMono<HttpsSendLog>
  9. {
  10. private Queue<string> qSendLog;
  11. private float times;
  12. Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
  13. private void Start()
  14. {
  15. requestHeader.Add("Content-Type", "application/json");
  16. qSendLog = new Queue<string>();
  17. times = 0;
  18. // SendLog("Error", "ceshi 11111111111");
  19. }
  20. public void SendLog(string tag,string message)
  21. {
  22. SendHttpsWriteLog sendHLog = new SendHttpsWriteLog();
  23. sendHLog.level = tag;
  24. sendHLog.tag = Application.productName;
  25. sendHLog.message = message;
  26. string json = JsonMapper.ToJson(sendHLog);
  27. qSendLog.Enqueue(json);
  28. }
  29. private void Update()
  30. {
  31. times += Time.deltaTime;
  32. if(times>1&&qSendLog.Count>0)
  33. {
  34. PostTest("", qSendLog.Dequeue());
  35. }
  36. }
  37. public void PostTest(string methodName, string jsonString)
  38. {
  39. StartCoroutine(PostRequestTest(methodName, jsonString));
  40. }
  41. private IEnumerator PostRequestTest(string methodName, string jsonString)
  42. {
  43. string url = "https://fat2.qulivr.com/mr-navigator/v1/unity/log";
  44. Debug.Log(url + jsonString);
  45. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  46. {
  47. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  48. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  49. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  50. foreach (var v in requestHeader)
  51. {
  52. webRequest.SetRequestHeader(v.Key, v.Value);
  53. }
  54. yield return webRequest.SendWebRequest();
  55. if (webRequest.error!=null)
  56. {
  57. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
  58. //GameManager.Instance.text2.text = webRequest.error;
  59. }
  60. else
  61. {
  62. Debug.Log(webRequest.downloadHandler.text);
  63. }
  64. }
  65. }
  66. }
  67. public class SendHttpsWriteLog
  68. {
  69. public string level { get; set; }
  70. public string tag { get; set; }
  71. public string message { get; set; }
  72. }