微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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。

Warning:会显示Warning,Assert,Error,Exception

Assert:会显示Assert,Error,Exception

Error:显示Error和Exception

Exception:只会显示Exception

 

使用

  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同级目录下

在打包发布后某些数据会获取不到 例如行号

StackFrame参考

最后看下效果

 

不足

发布版本 出现异常捕捉不到 行号获取不到

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

 

参考资料:

植物大战僵尸破解版: http://www.pvzbaike.com/archives/pvz_pojie/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐