欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何保存Unity中的Log日志

 更新時間:2021年04月09日 15:13:01   作者:q527955281  
這篇文章主要介紹了如何保存Unity中的Log日志的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

代碼中的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;
        }
    }
}

結果:

補充:Unity3D log寫入文件

關鍵代碼: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;
        }
    }
}
 

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關文章

  • 淺談C#中的Infinity和NaN

    淺談C#中的Infinity和NaN

    下面小編就為大家?guī)硪黄獪\談C#中的Infinity和NaN。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • 實例代碼講解c# 線程(下)

    實例代碼講解c# 線程(下)

    這篇文章主要介紹了c# 線程的的相關資料,文中示例代碼非常細致,對大家的學習有很大幫助,感興趣的朋友可以了解下
    2020-06-06
  • C#實現(xiàn)QQ窗口抖動效果

    C#實現(xiàn)QQ窗口抖動效果

    這篇文章主要為大家詳細介紹了C#實現(xiàn)QQ窗口抖動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • C# WPF 通過委托實現(xiàn)多窗口間的傳值的方法

    C# WPF 通過委托實現(xiàn)多窗口間的傳值的方法

    這篇文章主要介紹了C# WPF 通過委托實現(xiàn)多窗口間的傳值的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • C#使用IComparer自定義List類實現(xiàn)排序的方法

    C#使用IComparer自定義List類實現(xiàn)排序的方法

    這篇文章主要介紹了C#使用IComparer自定義List類實現(xiàn)排序的方法,涉及C#使用IComparer接口定義List類進行排序的相關技巧,需要的朋友可以參考下
    2015-08-08
  • C#創(chuàng)建安全的棧(Stack)存儲結構

    C#創(chuàng)建安全的棧(Stack)存儲結構

    這篇文章主要為大家詳細介紹了C#創(chuàng)建安全的棧(Stack)存儲結構的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • C#中const和readonly的用法比較

    C#中const和readonly的用法比較

    今天小編就為大家分享一篇關于C#中const和readonly的用法比較,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Unity實現(xiàn)物體弧線運動到規(guī)定的坐標

    Unity實現(xiàn)物體弧線運動到規(guī)定的坐標

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)物體以弧線的形式運動到規(guī)定的坐標,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • c# 獲取照片的經(jīng)緯度和時間的示例代碼

    c# 獲取照片的經(jīng)緯度和時間的示例代碼

    這篇文章主要介紹了c# 獲取照片的經(jīng)緯度和時間的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-11-11
  • Unity常用音頻操作類示例代碼

    Unity常用音頻操作類示例代碼

    這篇文章主要介紹了Unity常用音頻操作類,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07

最新評論