聊聊Unity 自定义日志保存的问题
作者:Zero_LJ 发布时间:2021-11-28 15:38:55
标签:Unity,日志,保存
前言
之前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 捕捉到更多信息
来源:https://blog.csdn.net/K20132014/article/details/86528716


猜你喜欢
- 1.EazyEmail邮件发送类库Net 类库自带了邮件发送功能。笔者对该类库,从使用的角度进行了二次封装,nuget上可搜索EazyEma
- 简述SpringBoot对Spring的的使用做了全面的封装,使用SpringBoot大大加快了开发进程,但是如果不了解Spring的特性,
- 本文实例为大家分享了Android倒计时效果的具体代码,供大家参考,具体内容如下需求:a.在后台添加时,如果是今日直播,则需要添加开始时间(
- 参考ColorComboBox做修改,并对颜色名做些修正,用于CR MVMixer产品中,聊作备忘~效果图:代码://颜色拾取框using
- 创建AlertDialog的步骤:1、创建AlertDialog.Builder对象2、调用Builder对象的setTitle
- 效果图:后来又出了两篇,也可以看一下Android选择与上传图片之PictureSelector教程Android选择与上传图片之Matis
- 首先感谢:http://www.codeproject.com/Articles/25487/Cryptographic-Interoper
- PipedOutputStream和PipedInputStream在java中,PipedOutputStream和PipedInputS
- 一、项目运行环境配置:Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,
- 本文实例分析了Android编程画图之抗锯齿解决方法。分享给大家供大家参考,具体如下:在画图的时候,图片如果旋转或缩放之后,总是会出现那些华
- 1. this 引用1.1 为什么要有this引用先来写一个日期类的例子:public class classCode { &
- mybatis多个区间处理如图:要实现车辆数不同区间查询条件思路a.前端传数组,数组里面放"1-5"String类型值
- 本文实例讲述了Android编程实现获得内存剩余大小与总大小的方法。分享给大家供大家参考,具体如下:public class memInfo
- 一、前言在日常工作中,如果涉及到与第三方进行接口对接,有的会使用WebService的方式,这篇文章主要讲解在.NET Framework中
- MyBatis-Generator自动生成映射文件生成的方式一共有三种1、使用cmd命令方式生成首先在generator.xml中指定数据库
- 本文实例讲述了Android使用SharedPreferences存储数据的实现方法。分享给大家供大家参考,具体如下:编辑短信的时候,突然接
- 代码很简单,这里就不多废话了,直接奉上代码using UnityEngine;using System.Collections;public
- 最近根据项目需要,整理了一个相对比较全面的 WheelView 使用控件,借用之前看到的一句话来说,就是站在巨人肩膀上,进行了一些小调整。
- 前文由于经常用到串口调试, 尽管有现成的软件, 因为前端时间涉及一个二次开发, 就因为一个RtsEnable设置, 折腾半天, 网上各种版本
- 网络应用分为客户端和服务端两部分,而Socket类是负责处理客户端通信的Java类。通过这个类可以连接到指定IP或域名的服务器上,并且可以和