聊聊Unity 自定义日志保存的问题
程序员文章站
2022-04-13 19:34:19
前言 之前unity5.x在代码中写了debug.log..等等,打包之后在当前程序文件夹下会有个对应的"outlog.txt",2017之后这个文件被移到c盘用户appdata/locallow/公...
前言
之前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 捕捉到更多信息
到此这篇关于聊聊unity 自定义日志保存的问题的文章就介绍到这了,更多相关unity日志保存内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!