Unity 自定義日誌保存

前言    

   以前unity5.x在代碼中寫了debug.log..等等,打包以後在當前程序文件夾下會有個對應的"outlog.txt",2017以後這個文件被移到C盤用戶Appdata/LocalLow/公司名 文件夾下面。以爲不方便就本身寫了個編輯器

代碼

  1.  
    using UnityEngine;
  2.  
    using System.IO;
  3.  
    using System;
  4.  
    using System.Diagnostics;
  5.  
    using Debug = UnityEngine.Debug;
  6.  
     
  7.  
     
  8.  
    public class DebugTrace
  9.  
    {
  10.  
    private FileStream fileStream;
  11.  
    private StreamWriter streamWriter;
  12.  
     
  13.  
    private bool isEditorCreate = false;//是否在編輯器中也產生日誌文件
  14.  
    private int showFrames = 1000; //打印全部
  15.  
     
  16.  
    #region instance
  17.  
    private static readonly object obj = new object();
  18.  
    private static DebugTrace m_instance;
  19.  
    public static DebugTrace Instance
  20.  
    {
  21.  
    get
  22.  
    {
  23.  
    if (m_instance == null)
  24.  
    {
  25.  
    lock (obj)
  26.  
    {
  27.  
    if (m_instance == null)
  28.  
    m_instance = new DebugTrace();
  29.  
    }
  30.  
    }
  31.  
    return m_instance;
  32.  
    }
  33.  
    }
  34.  
    #endregion
  35.  
     
  36.  
    private DebugTrace()
  37.  
    {
  38.  
     
  39.  
    }
  40.  
     
  41.  
     
  42.  
     
  43.  
    /// <summary>
  44.  
    /// 開啓跟蹤日誌信息
  45.  
    /// </summary>
  46.  
    public void StartTrace()
  47.  
    {
  48.  
    if (Debug.unityLogger.logEnabled)
  49.  
    {
  50.  
    if (Application.isEditor)
  51.  
    {
  52.  
    //在編輯器中設置isEditorCreate==true時候產生日誌
  53.  
    if (isEditorCreate)
  54.  
    {
  55.  
    CreateOutlog();
  56.  
    }
  57.  
    }
  58.  
    //不在編輯器中 是否產生日誌由 Debug.unityLogger.logEnabled 控制
  59.  
    else
  60.  
    {
  61.  
    CreateOutlog();
  62.  
    }
  63.  
    }
  64.  
    }
  65.  
    private void Application_logMessageReceivedThreaded(string logString, string stackTrace, LogType type)
  66.  
    {
  67.  
    // Debug.Log(stackTrace); //打包後staackTrace爲空 因此要本身實現
  68.  
    if (type != LogType.Warning)
  69.  
    {
  70.  
    // StackTrace stack = new StackTrace(1,true); //跳過第二?(1)幀
  71.  
    StackTrace stack = new StackTrace(true); //捕獲全部幀
  72.  
    string stackStr = string.Empty;
  73.  
     
  74.  
    int frameCount = stack.FrameCount; //幀數
  75.  
    if (this.showFrames > frameCount) this.showFrames = frameCount; //若是幀數大於總幀速 設置一下
  76.  
     
  77.  
    //自定義輸出幀數,能夠自行試試查看效果
  78.  
    for (int i = stack.FrameCount - this.showFrames; i < stack.FrameCount; i++)
  79.  
    {
  80.  
    StackFrame sf = stack.GetFrame(i); //獲取當前幀信息
  81.  
    // 1:第一種 ps:GetFileLineNumber 在發佈打包後獲取不到
  82.  
    stackStr += "at [" + sf.GetMethod().DeclaringType.FullName +
  83.  
    "." + sf.GetMethod().Name +
  84.  
    ".Line:" + sf.GetFileLineNumber() + "]\n ";
  85.  
     
  86.  
    //或者直接調用tostring 顯示數據過多 且打包後有些數據獲取不到
  87.  
    // stackStr += sf.ToString();
  88.  
    }
  89.  
     
  90.  
    //或者 stackStr = stack.ToString();
  91.  
    string content = string.Format("time: {0} logType: {1} logString: {2} \nstackTrace: {3} {4} ",
  92.  
    DateTime.Now.ToString( "HH:mm:ss"), type, logString, stackStr, "\r\n");
  93.  
    streamWriter.WriteLine(content);
  94.  
    streamWriter.Flush();
  95.  
    }
  96.  
    }
  97.  
    private void CreateOutlog()
  98.  
    {
  99.  
    if (!Directory.Exists(Application.dataPath + "/../" + "OutLog"))
  100.  
    Directory.CreateDirectory(Application.dataPath + "/../" + "OutLog");
  101.  
    string path = Application.dataPath + "/../OutLog" + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_log.txt";
  102.  
    fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  103.  
    streamWriter = new StreamWriter(fileStream);
  104.  
    Application.logMessageReceivedThreaded += Application_logMessageReceivedThreaded;
  105.  
    }
  106.  
     
  107.  
    /// <summary>
  108.  
    /// 關閉跟蹤日誌信息
  109.  
    /// </summary>
  110.  
    public void CloseTrace()
  111.  
    {
  112.  
    Application.logMessageReceivedThreaded -= Application_logMessageReceivedThreaded;
  113.  
    streamWriter.Dispose();
  114.  
    streamWriter.Close();
  115.  
    fileStream.Dispose();
  116.  
    fileStream.Close();
  117.  
    }
  118.  
    /// <summary>
  119.  
    /// 設置選項
  120.  
    /// </summary>
  121.  
    /// <param name="logEnable">是否記錄日誌</param>
  122.  
    /// <param name="showFrams">是否顯示全部堆棧幀 默認只顯示當前幀 若是設爲0 則顯示全部幀</param>
  123.  
    /// <param name="filterLogType">過濾 默認log級別以上</param>
  124.  
    /// <param name="editorCreate">是否在編輯器中產生日誌記錄 默認不須要</param>
  125.  
    public void SetLogOptions(bool logEnable, int showFrams = 1, LogType filterLogType = LogType.Log, bool editorCreate = false)
  126.  
    {
  127.  
    Debug.unityLogger.logEnabled = logEnable;
  128.  
    Debug.unityLogger.filterLogType = filterLogType;
  129.  
    isEditorCreate = editorCreate;
  130.  
    this.showFrames = showFrams == 0 ? 1000 : showFrams;
  131.  
    }
  132.  
     
  133.  
    }

關於 filterLogType

filterLogType默認設置是Log,會顯示全部類型的Log。ide

Warning:會顯示Warning,Assert,Error,Exceptionui

Assert:會顯示Assert,Error,Exceptionthis

Error:顯示Error和Exceptionspa

Exception:只會顯示Exceptiondebug

 

使用

  1.  
    using UnityEngine;
  2.  
     
  3.  
    public class Test : MonoBehaviour
  4.  
    {
  5.  
    private BoxCollider boxCollider;
  6.  
    void Start()
  7.  
    {
  8.  
    DebugTrace.Instance.SetLogOptions( true, 2, editorCreate: true); //設置日誌打開 顯示2幀 而且編輯器下產生日誌
  9.  
    DebugTrace.Instance.StartTrace();
  10.  
    Debug.Log( "log");
  11.  
    Debug.Log( "log", this);
  12.  
    Debug.LogError( "LogError");
  13.  
    Debug.LogAssertion( "LogAssertion");
  14.  
     
  15.  
    boxCollider.enabled = false; //報錯 發佈後捕捉不到幀
  16.  
    }
  17.  
     
  18.  
    private void OnApplicationQuit()
  19.  
    {
  20.  
    DebugTrace.Instance.CloseTrace();
  21.  
    }
  22.  
    }

若是在編輯器中也設置產生日誌,日誌文件在當前項目路徑下,打包後在exe同級目錄下日誌

在打包發佈後某些數據會獲取不到 例如行號code

StackFrame參考

最後看下效果:orm

 

不足

發佈版本 出現異常捕捉不到 行號獲取不到blog

debug版本能夠勾選DevelopMend build 捕捉到更多信息

 

參考資料:

植物大戰殭屍破解版: http://www.pvzbaike.com/archives/pvz_pojie/

相關文章
相關標籤/搜索