54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using ZooMatch;
|
|
|
|
|
|
|
|
public class ErrorLogger : MonoBehaviour
|
|
{
|
|
// 用于存储报错信息的列表
|
|
private List<string> errorLogs = new List<string>();
|
|
|
|
void OnEnable()
|
|
{
|
|
// 注册事件处理函数
|
|
Application.logMessageReceived += HandleLog;
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
// 注销事件处理函数
|
|
Application.logMessageReceived -= HandleLog;
|
|
}
|
|
|
|
// 事件处理函数
|
|
void HandleLog(string logString, string stackTrace, LogType type)
|
|
{
|
|
// 只处理错误和异常类型的日志
|
|
if (type == LogType.Error || type == LogType.Exception)
|
|
{
|
|
// 将报错信息和堆栈跟踪添加到列表中
|
|
errorLogs.Add(logString);
|
|
errorLogs.Add(stackTrace);
|
|
|
|
// 这里可以添加代码将报错信息发送给服务器
|
|
SendErrorToServer(logString, stackTrace);
|
|
}
|
|
}
|
|
|
|
// 如何将报错信息发送给服务器
|
|
void SendErrorToServer(string error, string stackTrace)
|
|
{
|
|
// Debug.Log($"SendErrorToServer-----------error\n{error}");
|
|
// Debug.Log($"SendErrorToServer-----------stackTrace\n{stackTrace}");
|
|
// 这里填写将报错信息发送到服务器的代码
|
|
var reqData = new RespDebugData
|
|
{
|
|
level = "error",
|
|
message = error,
|
|
stacktrace = stackTrace
|
|
};
|
|
NetworkKit.SendLogToServer(reqData);
|
|
}
|
|
}
|