您好,登錄后才能下訂單哦!
這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)使用Unity怎么自定義保存日志,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
using UnityEngine; using System.IO; using System; using System.Diagnostics; using Debug = UnityEngine.Debug; public class DebugTrace { private FileStream fileStream; private StreamWriter streamWriter; private bool isEditorCreate = false;//是否在編輯器中也產(chǎn)生日志文件 private int showFrames = 1000; //打印所有 #region instance private static readonly object obj = new object(); private static DebugTrace m_instance; public static DebugTrace Instance { get { if (m_instance == null) { lock (obj) { if (m_instance == null) m_instance = new DebugTrace(); } } return m_instance; } } #endregion private DebugTrace() { } /// <summary> /// 開啟跟蹤日志信息 /// </summary> public void StartTrace() { if (Debug.unityLogger.logEnabled) { if (Application.isEditor) { //在編輯器中設(shè)置isEditorCreate==true時候產(chǎn)生日志 if (isEditorCreate) { CreateOutlog(); } } //不在編輯器中 是否產(chǎn)生日志由 Debug.unityLogger.logEnabled 控制 else { CreateOutlog(); } } } private void Application_logMessageReceivedThreaded(string logString, string stackTrace, LogType type) { // Debug.Log(stackTrace); //打包后staackTrace為空 所以要自己實現(xiàn) if (type != LogType.Warning) { // StackTrace stack = new StackTrace(1,true); //跳過第二?(1)幀 StackTrace stack = new StackTrace(true); //捕獲所有幀 string stackStr = string.Empty; int frameCount = stack.FrameCount; //幀數(shù) if (this.showFrames > frameCount) this.showFrames = frameCount; //如果幀數(shù)大于總幀速 設(shè)置一下 //自定義輸出幀數(shù),可以自行試試查看效果 for (int i = stack.FrameCount - this.showFrames; i < stack.FrameCount; i++) { StackFrame sf = stack.GetFrame(i); //獲取當前幀信息 // 1:第一種 ps:GetFileLineNumber 在發(fā)布打包后獲取不到 stackStr += "at [" + sf.GetMethod().DeclaringType.FullName + "." + sf.GetMethod().Name + ".Line:" + sf.GetFileLineNumber() + "]\n "; //或者直接調(diào)用tostring 顯示數(shù)據(jù)過多 且打包后有些數(shù)據(jù)獲取不到 // stackStr += sf.ToString(); } //或者 stackStr = stack.ToString(); string content = string.Format("time: {0} logType: {1} logString: {2} \nstackTrace: {3} {4} ", DateTime.Now.ToString("HH:mm:ss"), type, logString, stackStr, "\r\n"); streamWriter.WriteLine(content); streamWriter.Flush(); } } private void CreateOutlog() { if (!Directory.Exists(Application.dataPath + "/../" + "OutLog")) Directory.CreateDirectory(Application.dataPath + "/../" + "OutLog"); string path = Application.dataPath + "/../OutLog" + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_log.txt"; fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); streamWriter = new StreamWriter(fileStream); Application.logMessageReceivedThreaded += Application_logMessageReceivedThreaded; } /// <summary> /// 關(guān)閉跟蹤日志信息 /// </summary> public void CloseTrace() { Application.logMessageReceivedThreaded -= Application_logMessageReceivedThreaded; streamWriter.Dispose(); streamWriter.Close(); fileStream.Dispose(); fileStream.Close(); } /// <summary> /// 設(shè)置選項 /// </summary> /// <param name="logEnable">是否記錄日志</param> /// <param name="showFrams">是否顯示所有堆棧幀 默認只顯示當前幀 如果設(shè)為0 則顯示所有幀</param> /// <param name="filterLogType">過濾 默認log級別以上</param> /// <param name="editorCreate">是否在編輯器中產(chǎn)生日志記錄 默認不需要</param> public void SetLogOptions(bool logEnable, int showFrams = 1, LogType filterLogType = LogType.Log, bool editorCreate = false) { Debug.unityLogger.logEnabled = logEnable; Debug.unityLogger.filterLogType = filterLogType; isEditorCreate = editorCreate; this.showFrames = showFrams == 0 ? 1000 : showFrams; } }
filterLogType默認設(shè)置是Log,會顯示所有類型的Log。
Warning:會顯示W(wǎng)arning,Assert,Error,Exception
Assert:會顯示Assert,Error,Exception
Error:顯示Error和Exception
Exception:只會顯示Exception
using UnityEngine; public class Test : MonoBehaviour { private BoxCollider boxCollider; void Start() { DebugTrace.Instance.SetLogOptions(true, 2, editorCreate: true); //設(shè)置日志打開 顯示2幀 并且編輯器下產(chǎn)生日志 DebugTrace.Instance.StartTrace(); Debug.Log("log"); Debug.Log("log", this); Debug.LogError("LogError"); Debug.LogAssertion("LogAssertion"); boxCollider.enabled = false; //報錯 發(fā)布后捕捉不到幀 } private void OnApplicationQuit() { DebugTrace.Instance.CloseTrace(); } }
如果在編輯器中也設(shè)置產(chǎn)生日志,日志文件在當前項目路徑下,打包后在exe同級目錄下
在打包發(fā)布后某些數(shù)據(jù)會獲取不到 例如行號
最后看下效果:
發(fā)布版本 出現(xiàn)異常捕捉不到 行號獲取不到
debug版本可以勾選DevelopMend build 捕捉到更多信息
上述就是小編為大家分享的使用Unity怎么自定義保存日志了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。