Unity 自定义日志保存

游戏 游戏 1249 人阅读 | 0 人回复

<
媒介    

   之前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. public class DebugTrace
  7. {
  8.     private FileStream fileStream;
  9.     private StreamWriter streamWriter;
  10.     private bool isEditorCreate = false;//能否正在编纂器中也发生日记文件
  11.     private int showFrames = 1000;  //挨印一切
  12.     #region instance
  13.     private static readonly object obj = new object();
  14.     private static DebugTrace m_instance;
  15.     public static DebugTrace Instance
  16.     {
  17.         get
  18.         {
  19.             if (m_instance == null)
  20.             {
  21.                 lock (obj)
  22.                 {
  23.                     if (m_instance == null)
  24.                         m_instance = new DebugTrace();
  25.                 }
  26.             }
  27.             return m_instance;
  28.         }
  29.     }
  30.     #endregion
  31.     private DebugTrace()
  32.     {
  33.     }
  34.     /// <summary>
  35.     /// 开启跟踪日记疑息
  36.     /// </summary>
  37.     public void StartTrace()
  38.     {
  39.         if (Debug.unityLogger.logEnabled)
  40.         {
  41.             if (Application.isEditor)
  42.             {
  43.                 //正在编纂器中设置isEditorCreate==true时分发生日记
  44.                 if (isEditorCreate)
  45.                 {
  46.                     CreateOutlog();
  47.                 }
  48.             }
  49.             //没有正在编纂器中 能否发生日记由  Debug.unityLogger.logEnabled 掌握
  50.             else
  51.             {
  52.                 CreateOutlog();
  53.             }
  54.         }
  55.     }
  56.     private void Application_logMessageReceivedThreaded(string logString, string stackTrace, LogType type)
  57.     {
  58.         //  Debug.Log(stackTrace);  //挨包后staackTrace为空 以是要本人完成
  59.         if (type != LogType.Warning)
  60.         {
  61.             // StackTrace stack = new StackTrace(1,true); //跳过第两?(1)帧
  62.             StackTrace stack = new StackTrace(true);  //捕捉一切帧
  63.             string stackStr = string.Empty;
  64.             int frameCount = stack.FrameCount;  //帧数
  65.             if (this.showFrames > frameCount) this.showFrames = frameCount;  //假如帧数年夜于总帧速 设置一下
  66.             //自界说输出帧数,能够自止尝尝检察结果
  67.             for (int i = stack.FrameCount - this.showFrames; i < stack.FrameCount; i++)
  68.             {
  69.                 StackFrame sf = stack.GetFrame(i);  //获得当前帧疑息
  70.                                                     // 1:第一种    ps:GetFileLineNumber 正在公布挨包后获得没有到
  71.                 stackStr += "at [" + sf.GetMethod().DeclaringType.FullName +
  72.                             "." + sf.GetMethod().Name +
  73.                             ".Line:" + sf.GetFileLineNumber() + "]\n            ";
  74.                 //大概间接挪用tostring 显现数据过量 且挨包后有些数据获得没有到
  75.                 // stackStr += sf.ToString();
  76.             }
  77.             //大概 stackStr = stack.ToString();
  78.             string content = string.Format("time: {0}   logType: {1}    logString: {2} \nstackTrace: {3} {4} ",
  79.                                                DateTime.Now.ToString("HH:mm:ss"), type, logString, stackStr, "\r\n");
  80.             streamWriter.WriteLine(content);
  81.             streamWriter.Flush();
  82.         }
  83.     }
  84.     private void CreateOutlog()
  85.     {
  86.         if (!Directory.Exists(Application.dataPath + "/../" + "OutLog"))
  87.             Directory.CreateDirectory(Application.dataPath + "/../" + "OutLog");
  88.         string path = Application.dataPath + "/../OutLog" + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_log.txt";
  89.         fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  90.         streamWriter = new StreamWriter(fileStream);
  91.         Application.logMessageReceivedThreaded += Application_logMessageReceivedThreaded;
  92.     }
  93.     /// <summary>
  94.     /// 封闭跟踪日记疑息
  95.     /// </summary>
  96.     public void CloseTrace()
  97.     {
  98.         Application.logMessageReceivedThreaded -= Application_logMessageReceivedThreaded;
  99.         streamWriter.Dispose();
  100.         streamWriter.Close();
  101.         fileStream.Dispose();
  102.         fileStream.Close();
  103.     }
  104.     /// <summary>
  105.     /// 设置选项
  106.     /// </summary>
  107.     /// <param name="logEnable">能否记载日记</param>
  108.     /// <param name="showFrams">能否显现一切仓库帧 默许只显现当前帧 假如设为0 则显现一切帧</param>
  109.     /// <param name="filterLogType">过滤 默许log级别以上</param>
  110.     /// <param name="editorCreate">能否正在编纂器中发生日记记载 默许没有需求</param>
  111.     public void SetLogOptions(bool logEnable, int showFrams = 1, LogType filterLogType = LogType.Log, bool editorCreate = false)
  112.     {
  113.         Debug.unityLogger.logEnabled = logEnable;
  114.         Debug.unityLogger.filterLogType = filterLogType;
  115.         isEditorCreate = editorCreate;
  116.         this.showFrames = showFrams == 0 ? 1000 : showFrams;
  117.     }
  118. }
复造代码
闭于 filterLogType

filterLogType默许设置是Log,会显现一切规范的Log。
Warning:会显现Warning,Assert,Error,Exception
Assert:会显现Assert,Error,Exception
Error:显现Error战Exception
Exception:只会显现Exception
 
利用

  1. using UnityEngine;
  2. public class Test : MonoBehaviour
  3. {
  4.     private BoxCollider boxCollider;
  5.     void Start()
  6.     {
  7.         DebugTrace.Instance.SetLogOptions(true, 2, editorCreate: true); //设置日记翻开 显现2帧 而且编纂器下发生日记
  8.         DebugTrace.Instance.StartTrace();
  9.         Debug.Log("log");
  10.         Debug.Log("log", this);
  11.         Debug.LogError("LogError");
  12.         Debug.LogAssertion("LogAssertion");
  13.       
  14.         boxCollider.enabled = false;  //报错 公布后捕获没有到帧
  15.     }
  16.     private void OnApplicationQuit()
  17.     {
  18.         DebugTrace.Instance.CloseTrace();
  19.     }
  20. }
复造代码
假如正在编纂器中也设置发生日记,日记文件正在当前项目途径下,挨包后正在exe同级目次下
正在挨包公布后某些数据会获得没有到 比方止号
StackFrame参考

155036tvyb39kwk6e3k42z.jpg

最初看下结果:
155037uf7ftm7pzdmffif1.jpg

 
不敷

公布版本 呈现非常捕获没有到 止号获得没有到
debug版本能够勾选DevelopMend build 捕获到更多疑息
155037ebrjra8gkgi8cbvj.jpg


免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作!
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
回复 关闭延时

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则