最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

聊聊Unity 自定義日志保存的問題

 更新時間:2021年05月12日 16:02:25   作者:Zero_LJ  
這篇文章主要介紹了Unity 自定義日志保存的問題,之前unity5.x在代碼中寫了debug.log打包之后在當(dāng)前程序文件夾下會有個對應(yīng)的"outlog.txt",后來進行了更改,今天通過代碼給大家介紹了Unity日志保存的問題,需要的朋友一起看看吧

前言    

   之前unity5.x在代碼中寫了debug.log..等等,打包之后在當(dāng)前程序文件夾下會有個對應(yīng)的"outlog.txt",2017之后這個文件被移到C盤用戶Appdata/LocalLow/公司名 文件夾下面。覺得不方便就自己寫了個

代碼

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);  //獲取當(dāng)前幀信息
                                                    // 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">是否顯示所有堆棧幀 默認只顯示當(dāng)前幀 如果設(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;
    }
 
}

關(guān)于 filterLogType

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)生日志,日志文件在當(dāng)前項目路徑下,打包后在exe同級目錄下

在打包發(fā)布后某些數(shù)據(jù)會獲取不到 例如行號

StackFrame參考

最后看下效果:

不足

發(fā)布版本 出現(xiàn)異常捕捉不到 行號獲取不到

debug版本可以勾選DevelopMend build 捕捉到更多信息

到此這篇關(guān)于聊聊Unity 自定義日志保存的問題的文章就介紹到這了,更多相關(guān)Unity日志保存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#9.0主要特性的一些想法

    C#9.0主要特性的一些想法

    這篇文章主要給大家介紹了關(guān)于C#9.0主要特性的一些想法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • C#連續(xù)任務(wù)Task.ContinueWith方法

    C#連續(xù)任務(wù)Task.ContinueWith方法

    這篇文章介紹了C#中的連續(xù)任務(wù)Task.ContinueWith方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#?使用Fluent?API?創(chuàng)建自己的DSL(推薦)

    C#?使用Fluent?API?創(chuàng)建自己的DSL(推薦)

    DSL領(lǐng)域?qū)S谜Z言是描述特定領(lǐng)域問題的語言,聽起來很唬人,其實不是什么高深的東西,下面通過實例代碼介紹下C#?使用Fluent?API?創(chuàng)建自己的DSL,感興趣的朋友參考下吧
    2021-12-12
  • C#異步的世界(上)

    C#異步的世界(上)

    這篇文章主要介紹了C#異步的世界,對異步感興趣的同學(xué),可以參考下
    2021-04-04
  • C#實現(xiàn)俄羅斯方塊

    C#實現(xiàn)俄羅斯方塊

    這篇文章主要為大家詳細介紹了C#實現(xiàn)俄羅斯方塊小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • C#利用子線程刷新主線程分享教程

    C#利用子線程刷新主線程分享教程

    本文將詳細介紹C#利用子線程如何刷新主線程,需要了解更多的朋友可以參考下
    2012-11-11
  • C#程序(含多個Dll)合并成一個Exe的簡單方法

    C#程序(含多個Dll)合并成一個Exe的簡單方法

    這篇文章主要為大家詳細介紹了C#程序(含多個Dll)合并成一個Exe的簡單方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • C# winform打開Excel文檔的方法總結(jié)(必看篇)

    C# winform打開Excel文檔的方法總結(jié)(必看篇)

    下面小編就為大家?guī)硪黄狢# winform打開Excel文檔的方法總結(jié)(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Audio Source組件及相關(guān)API

    Audio Source組件及相關(guān)API

    這篇文章主要介紹了Audio Source組件及相關(guān)API的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-11-11
  • 使用c#實現(xiàn)微信自動化功能

    使用c#實現(xiàn)微信自動化功能

    這篇文章主要介紹了使用c#實現(xiàn)微信自動化,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08

最新評論

丹凤县| 荔波县| 明溪县| 信丰县| 施秉县| 江陵县| 元朗区| 酒泉市| 越西县| 宁晋县| 兴仁县| 京山县| 鄂州市| 汪清县| 藁城市| 沙田区| 黔江区| 霍山县| 崇礼县| 宁远县| 定兴县| 秭归县| 乌兰县| 宁河县| 仁化县| 赣榆县| 大姚县| 彭阳县| 鸡西市| 衡东县| 黄浦区| 新竹市| 木兰县| 长沙市| 剑川县| 凤庆县| 久治县| 盐津县| 故城县| 永仁县| 常德市|