如何保存Unity中的Log日志
更新時(shí)間:2021年04月09日 15:13:01 作者:q527955281
這篇文章主要介紹了如何保存Unity中的Log日志的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
代碼中的debug日志保存本地
using System.Collections; using UnityEngine; using System.IO; public class SaveLog : MonoBehaviour { private float length; Queue queue; private void Awake() { DontDestroyOnLoad(this); LogToFile("Version of the runtime: " + Application.unityVersion, true, false); Application.logMessageReceivedThreaded += OnReceiveLogMsg; queue = new Queue(); } // Start is called before the first frame update void OnReceiveLogMsg(string condition, string stackTrace, LogType type) { string _type = ""; switch (type) { case LogType.Error: _type = "error"; break; case LogType.Assert: _type = "Assert"; break; case LogType.Warning: _type = "Warning"; break; case LogType.Log: _type = "Log"; break; case LogType.Exception: _type = "Exception"; break; default: break; } string msg = "[MSG]:" + condition + "--" + "[station]:" + stackTrace + "-" + "[LogType]:" + _type; queue.Enqueue(msg); } // Update is called once per frame void Update() { CheckLogs(); } void CheckLogs() { if (queue.Count != 0) { LogToFile(queue.Peek().ToString(), true, true,()=> { queue.Dequeue(); }); } } public void LogToFile(string str, bool bwithTime, bool bAppendLineFeed,System.Action callback = null) { if (str == null) return; try { #if UNITY_EDITOR string fname = Application.dataPath + "/Unitylog.txt"; #else string fname = Application.persistentDataPath+ "/Unitylog.txt"; #endif if (fname == "" || fname == null) return; StreamWriter writer = new StreamWriter(fname, true, System.Text.Encoding.Default); if (bwithTime) writer.WriteLine("\r\n\r\n---------" + System.DateTime.Now.ToString()); if (bAppendLineFeed) writer.WriteLine(str); else writer.Write(str); writer.Close(); callback?.Invoke(); } catch { throw; } } }
結(jié)果:
補(bǔ)充:Unity3D log寫(xiě)入文件
關(guān)鍵代碼:Application.RegisterLogCallback(logCallBack);
using UnityEngine; using System.IO; public class Logger { string fullPath; public void InitLogger() { fullPath = Application.dataPath + "/output.txt"; if (File.Exists(fullPath)) File.Delete(fullPath); Debug.Log(fullPath.Replace("/output.txt", "")); if (Directory.Exists(fullPath.Replace("/output.txt", ""))) { FileStream fs = File.Create(fullPath); fs.Close(); Application.logMessageReceived += logCallBack; } else { Debug.LogError("directory is not exist"); } } private void logCallBack(string condition, string stackTrace, LogType type) { if (File.Exists(fullPath)) { using (StreamWriter sw = File.AppendText(fullPath)) { sw.WriteLine(condition); sw.WriteLine(stackTrace); } } } private static Logger s_instance; public static Logger instance { get { if (null == s_instance) s_instance = new Logger(); return s_instance; } } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
C# WPF 通過(guò)委托實(shí)現(xiàn)多窗口間的傳值的方法
這篇文章主要介紹了C# WPF 通過(guò)委托實(shí)現(xiàn)多窗口間的傳值的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09C#使用IComparer自定義List類(lèi)實(shí)現(xiàn)排序的方法
這篇文章主要介紹了C#使用IComparer自定義List類(lèi)實(shí)現(xiàn)排序的方法,涉及C#使用IComparer接口定義List類(lèi)進(jìn)行排序的相關(guān)技巧,需要的朋友可以參考下2015-08-08C#創(chuàng)建安全的棧(Stack)存儲(chǔ)結(jié)構(gòu)
這篇文章主要為大家詳細(xì)介紹了C#創(chuàng)建安全的棧(Stack)存儲(chǔ)結(jié)構(gòu)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Unity實(shí)現(xiàn)物體弧線運(yùn)動(dòng)到規(guī)定的坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體以弧線的形式運(yùn)動(dòng)到規(guī)定的坐標(biāo),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06c# 獲取照片的經(jīng)緯度和時(shí)間的示例代碼
這篇文章主要介紹了c# 獲取照片的經(jīng)緯度和時(shí)間的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11