LogServer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using DefaultNamespace.LogTool;
  9. using UnityEngine;
  10. using Utility;
  11. public class LogServer : Singleton<LogServer>
  12. {
  13. public string playerID;
  14. public string gameName = "Editor";
  15. public string version;
  16. private string deviceModel;
  17. private string operatingSystem;
  18. private Dictionary<string, float> _hashDic = new Dictionary<string, float>();
  19. public void Init(string gameName)
  20. {
  21. this.gameName = gameName;
  22. version = Application.version;
  23. deviceModel = SystemInfo.deviceName + " " + SystemInfo.deviceModel + " " + SystemInfo.deviceUniqueIdentifier;
  24. operatingSystem = SystemInfo.operatingSystem;
  25. }
  26. public void UpLog(Exception exception)
  27. {
  28. try
  29. {
  30. LogServeData logServeData = new LogServeData();
  31. logServeData.hash = GetStackTraceHash(exception);
  32. logServeData.title = exception.Message;
  33. logServeData.content = exception.StackTrace;
  34. logServeData.LogType = 1;
  35. logServeData.gameName = gameName;
  36. // logServeData.time = (long)(CombatTimerManager.Instance.TimeSinceStartUp() * 1000);
  37. logServeData.playerId = playerID;
  38. logServeData.v = version;
  39. logServeData.DeviceID = deviceModel;
  40. logServeData.operatingSystem = operatingSystem;
  41. UpdatToServer(logServeData);
  42. }
  43. catch (Exception e)
  44. {
  45. }
  46. }
  47. public void UpLog(string title, string messge)
  48. {
  49. try
  50. {
  51. LogServeData logServeData = new LogServeData();
  52. logServeData.hash = GetStackTraceHash(title, messge);
  53. logServeData.title = title;
  54. logServeData.content = messge;
  55. logServeData.LogType = 2;
  56. logServeData.gameName = gameName;
  57. // logServeData.time = (long)(CombatTimerManager.Instance.TimeSinceStartUp() * 1000);
  58. logServeData.playerId = playerID;
  59. logServeData.v = version;
  60. logServeData.DeviceID = deviceModel;
  61. logServeData.operatingSystem = operatingSystem;
  62. UpdatToServer(logServeData);
  63. }
  64. catch (Exception e)
  65. {
  66. }
  67. }
  68. private async Task UpdatToServer(LogServeData logServeData)
  69. {
  70. float ct = Time.time;
  71. if (_hashDic.Count > 200)
  72. {
  73. _hashDic.Clear();
  74. }
  75. if (_hashDic.TryGetValue(logServeData.hash, out float t))
  76. {
  77. if ((ct - t) < 10)
  78. {
  79. return;
  80. }
  81. _hashDic[logServeData.hash] = ct;
  82. }
  83. else
  84. {
  85. _hashDic.Add(logServeData.hash, t);
  86. }
  87. string json = LitJson.JsonMapper.ToJson(logServeData);
  88. byte[] logData = Encoding.UTF8.GetBytes(json);
  89. using var httpClient = new HttpClient();
  90. var content = new ByteArrayContent(logData);
  91. content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
  92. var response = await httpClient.PostAsync("http://139.155.99.185:5001/api/logserver/upload", content);
  93. if (response.IsSuccessStatusCode)
  94. {
  95. var result = await response.Content.ReadAsStringAsync();
  96. // UnityEngine.Debug.Log($"上传成功: {result}");
  97. }
  98. // else
  99. // {
  100. // UnityEngine.Debug.Log($"上传失败: {response.StatusCode}");
  101. // }
  102. }
  103. private string GetStackTraceHash(string title, string messge)
  104. {
  105. var sb = new StringBuilder();
  106. sb.Append(title);
  107. sb.Append(messge);
  108. return GetStackTraceHash(sb);
  109. }
  110. private string GetStackTraceHash(Exception ex)
  111. {
  112. var trace = new StackTrace(ex, true);
  113. var sb = new StringBuilder();
  114. foreach (var frame in trace.GetFrames() ?? Array.Empty<StackFrame>())
  115. {
  116. var method = frame.GetMethod();
  117. if (method?.DeclaringType != null)
  118. {
  119. sb.Append(method.DeclaringType.FullName)
  120. .Append(".")
  121. .Append(method.Name)
  122. .Append("()");
  123. }
  124. }
  125. return GetStackTraceHash(sb);
  126. }
  127. private string GetStackTraceHash(StringBuilder stringBuilder)
  128. {
  129. using var sha = System.Security.Cryptography.SHA1.Create();
  130. byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(stringBuilder.ToString()));
  131. return Convert.ToBase64String(hash);
  132. }
  133. }