前言
以前unity5.x在代碼中寫了debug.log..等等,打包以後在當前程序文件夾下會有個對應的"outlog.txt",2017以後這個文件被移到C盤用戶Appdata/LocalLow/公司名 文件夾下面。以爲不方便就本身寫了個編輯器
代碼
-
-
-
-
using System.Diagnostics;
-
using Debug = UnityEngine.Debug;
-
-
-
-
-
private FileStream fileStream;
-
private StreamWriter streamWriter;
-
-
private bool isEditorCreate = false;
-
private int showFrames = 1000;
-
-
-
private static readonly object obj = new object();
-
private static DebugTrace m_instance;
-
public static DebugTrace Instance
-
-
-
-
-
-
-
-
-
m_instance =
new DebugTrace();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
if (Debug.unityLogger.logEnabled)
-
-
if (Application.isEditor)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
private void Application_logMessageReceivedThreaded(string logString, string stackTrace, LogType type)
-
-
-
if (type != LogType.Warning)
-
-
-
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);
-
-
stackStr +=
"at [" + sf.GetMethod().DeclaringType.FullName +
-
"." + sf.GetMethod().Name +
-
".Line:" + sf.GetFileLineNumber() + "]\n ";
-
-
-
-
-
-
-
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);
-
-
-
-
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;
-
-
-
-
-
-
-
-
Application.logMessageReceivedThreaded -= Application_logMessageReceivedThreaded;
-
-
-
-
-
-
-
-
-
-
-
-
-
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
filterLogType默認設置是Log,會顯示全部類型的Log。ide
Warning:會顯示Warning,Assert,Error,Exceptionui
Assert:會顯示Assert,Error,Exceptionthis
Error:顯示Error和Exceptionspa
Exception:只會顯示Exceptiondebug
使用
-
-
-
public class Test : MonoBehaviour
-
-
private BoxCollider boxCollider;
-
-
-
DebugTrace.Instance.SetLogOptions(
true, 2, editorCreate: true);
-
DebugTrace.Instance.StartTrace();
-
-
-
Debug.LogError(
"LogError");
-
Debug.LogAssertion(
"LogAssertion");
-
-
boxCollider.enabled =
false;
-
-
-
private void OnApplicationQuit()
-
-
DebugTrace.Instance.CloseTrace();
-
-
若是在編輯器中也設置產生日誌,日誌文件在當前項目路徑下,打包後在exe同級目錄下日誌
在打包發佈後某些數據會獲取不到 例如行號code
StackFrame參考
![](http://static.javashuo.com/static/loading.gif)
最後看下效果:orm
![](http://static.javashuo.com/static/loading.gif)
不足
發佈版本 出現異常捕捉不到 行號獲取不到blog
debug版本能夠勾選DevelopMend build 捕捉到更多信息
![](http://static.javashuo.com/static/loading.gif)
參考資料:
植物大戰殭屍破解版: http://www.pvzbaike.com/archives/pvz_pojie/