以前unity5.x在代碼中寫了debug.log..等等,打包以後在當前程序文件夾下會有個對應的"outlog.txt",2017以後這個文件被移到C盤用戶Appdata/LocalLow/公司名 文件夾下面。以爲不方便就本身寫了個bash
代碼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;//是否在編輯器中也產生日誌文件 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) { //在編輯器中設置isEditorCreate==true時候產生日誌 if (isEditorCreate) { CreateOutlog(); } } //不在編輯器中 是否產生日誌由 Debug.unityLogger.logEnabled 控制 else { CreateOutlog(); } } } private void Application_logMessageReceivedThreaded(string logString, string stackTrace, LogType type) { // Debug.Log(stackTrace); //打包後staackTrace爲空 因此要本身實現 if (type != LogType.Warning) { // StackTrace stack = new StackTrace(1,true); //跳過第二?(1)幀 StackTrace stack = new StackTrace(true); //捕獲全部幀 string stackStr = string.Empty; int frameCount = stack.FrameCount; //幀數 if (this.showFrames > frameCount) this.showFrames = frameCount; //若是幀數大於總幀速 設置一下 //自定義輸出幀數,能夠自行試試查看效果 for (int i = stack.FrameCount - this.showFrames; i < stack.FrameCount; i++) { StackFrame sf = stack.GetFrame(i); //獲取當前幀信息 // 1:第一種 ps:GetFileLineNumber 在發佈打包後獲取不到 stackStr += "at [" + sf.GetMethod().DeclaringType.FullName + "." + sf.GetMethod().Name + ".Line:" + sf.GetFileLineNumber() + "]\n "; //或者直接調用tostring 顯示數據過多 且打包後有些數據獲取不到 // 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> /// 關閉跟蹤日誌信息 /// </summary> public void CloseTrace() { Application.logMessageReceivedThreaded -= Application_logMessageReceivedThreaded; streamWriter.Dispose(); streamWriter.Close(); fileStream.Dispose(); fileStream.Close(); } /// <summary> /// 設置選項 /// </summary> /// <param name="logEnable">是否記錄日誌</param> /// <param name="showFrams">是否顯示全部堆棧幀 默認只顯示當前幀 若是設爲0 則顯示全部幀</param> /// <param name="filterLogType">過濾 默認log級別以上</param> /// <param name="editorCreate">是否在編輯器中產生日誌記錄 默認不須要</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; } }
關於 filterLogTypeapp
filterLogType默認設置是Log,會顯示全部類型的Log。編輯器
Warning:會顯示Warning,Assert,Error,Exceptionide
Assert:會顯示Assert,Error,Exceptionui
Error:顯示Error和Exceptionthis
Exception:只會顯示Exceptionspa
使用debug
using UnityEngine; public class Test : MonoBehaviour { private BoxCollider boxCollider; void Start() { DebugTrace.Instance.SetLogOptions(true, 2, editorCreate: true); //設置日誌打開 顯示2幀 而且編輯器下產生日誌 DebugTrace.Instance.StartTrace(); Debug.Log("log"); Debug.Log("log", this); Debug.LogError("LogError"); Debug.LogAssertion("LogAssertion"); boxCollider.enabled = false; //報錯 發佈後捕捉不到幀 } private void OnApplicationQuit() { DebugTrace.Instance.CloseTrace(); } }
若是在編輯器中也設置產生日誌,日誌文件在當前項目路徑下,打包後在exe同級目錄下日誌
在打包發佈後某些數據會獲取不到 例如行號orm
StackFrame參考最後看下效果:
不足發佈版本 出現異常捕捉不到 行號獲取不到
debug版本能夠勾選DevelopMend build 捕捉到更多信息
圖片來源於:頁遊