|
<
媒介
之前unity5.x正在代码中写了debug.log..等等,挨包以后正在当出息叙文件夹下会有个对应的"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;//能否正在编纂器中也发生日记文件
- 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)
- {
- //正在编纂器中设置isEditorCreate==true时分发生日记
- if (isEditorCreate)
- {
- CreateOutlog();
- }
- }
- //没有正在编纂器中 能否发生日记由 Debug.unityLogger.logEnabled 掌握
- else
- {
- CreateOutlog();
- }
- }
- }
- private void Application_logMessageReceivedThreaded(string logString, string stackTrace, LogType type)
- {
- // Debug.Log(stackTrace); //挨包后staackTrace为空 以是要本人完成
- if (type != LogType.Warning)
- {
- // StackTrace stack = new StackTrace(1,true); //跳过第两?(1)帧
- StackTrace stack = new StackTrace(true); //捕捉一切帧
- string stackStr = string.Empty;
- int frameCount = stack.FrameCount; //帧数
- if (this.showFrames > frameCount) this.showFrames = frameCount; //假如帧数年夜于总帧速 设置一下
- //自界说输出帧数,能够自止尝尝检察结果
- for (int i = stack.FrameCount - this.showFrames; i < stack.FrameCount; i++)
- {
- StackFrame sf = stack.GetFrame(i); //获得当前帧疑息
- // 1:第一种 ps:GetFileLineNumber 正在公布挨包后获得没有到
- stackStr += "at [" + sf.GetMethod().DeclaringType.FullName +
- "." + sf.GetMethod().Name +
- ".Line:" + sf.GetFileLineNumber() + "]\n ";
- //大概间接挪用tostring 显现数据过量 且挨包后有些数据获得没有到
- // 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>
- /// 封闭跟踪日记疑息
- /// </summary>
- public void CloseTrace()
- {
- Application.logMessageReceivedThreaded -= Application_logMessageReceivedThreaded;
- streamWriter.Dispose();
- streamWriter.Close();
- fileStream.Dispose();
- fileStream.Close();
- }
- /// <summary>
- /// 设置选项
- /// </summary>
- /// <param name="logEnable">能否记载日记</param>
- /// <param name="showFrams">能否显现一切仓库帧 默许只显现当前帧 假如设为0 则显现一切帧</param>
- /// <param name="filterLogType">过滤 默许log级别以上</param>
- /// <param name="editorCreate">能否正在编纂器中发生日记记载 默许没有需求</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;
- }
- }
复造代码 闭于 filterLogType
filterLogType默许设置是Log,会显现一切规范的Log。
Warning:会显现Warning,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); //设置日记翻开 显现2帧 而且编纂器下发生日记
- DebugTrace.Instance.StartTrace();
- Debug.Log("log");
- Debug.Log("log", this);
- Debug.LogError("LogError");
- Debug.LogAssertion("LogAssertion");
-
- boxCollider.enabled = false; //报错 公布后捕获没有到帧
- }
- private void OnApplicationQuit()
- {
- DebugTrace.Instance.CloseTrace();
- }
- }
复造代码 假如正在编纂器中也设置发生日记,日记文件正在当前项目途径下,挨包后正在exe同级目次下
正在挨包公布后某些数据会获得没有到 比方止号
StackFrame参考
最初看下结果:
不敷
公布版本 呈现非常捕获没有到 止号获得没有到
debug版本能够勾选DevelopMend build 捕获到更多疑息
免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作! |
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
|