这篇文章主要介绍了如何保存Unity中的Log日志的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
代码中的debug日志保存本地
using System.Collections;
using UnityEngine;
using System.IO;
public class SaveLog : MonoBehaviour
{
private float length;
Queue queue;
private void Awake()
{
DontDestroyOnLoad(this);
LogToFile("Version of the runtime: " + Application.unityVersion, true, false);
Application.logMessageReceivedThreaded += OnReceiveLogMsg;
queue = new Queue();
}
// Start is called before the first frame update
void OnReceiveLogMsg(string condition, string stackTrace, LogType type) {
string _type = "";
switch (type)
{
case LogType.Error:
_type = "error";
break;
case LogType.Assert:
_type = "Assert";
break;
case LogType.Warning:
_type = "Warning";
break;
case LogType.Log:
_type = "Log";
break;
case LogType.Exception:
_type = "Exception";
break;
default:
break;
}
string msg = "[MSG]:" + condition + "--" + "[station]:" + stackTrace + "-" + "[LogType]:" + _type;
queue.Enqueue(msg);
}
// Update is called once per frame
void Update()
{
CheckLogs();
}
void CheckLogs() {
if (queue.Count != 0) {
LogToFile(queue.Peek().ToString(), true, true,()=> { queue.Dequeue(); });
}
}
public void LogToFile(string str, bool bwithTime, bool bAppendLineFeed,System.Action callback = null) {
if (str == null) return;
try
{
#if UNITY_EDITOR
string fname = Application.dataPath + "/Unitylog.txt";
#else
string fname = Application.persistentDataPath+ "/Unitylog.txt";
#endif
if (fname == "" || fname == null) return;
StreamWriter writer = new StreamWriter(fname, true, System.Text.Encoding.Default);
if (bwithTime) writer.WriteLine("\r\n\r\n---------" + System.DateTime.Now.ToString());
if (bAppendLineFeed) writer.WriteLine(str);
else writer.Write(str);
writer.Close();
callback?.Invoke();
}
catch
{
throw;
}
}
}
结果:
补充:Unity3D log写入文件
关键代码:Application.RegisterLogCallback(logCallBack);
using UnityEngine;
using System.IO;
public class Logger
{
string fullPath;
public void InitLogger()
{
fullPath = Application.dataPath + "/output.txt";
if (File.Exists(fullPath)) File.Delete(fullPath);
Debug.Log(fullPath.Replace("/output.txt", ""));
if (Directory.Exists(fullPath.Replace("/output.txt", "")))
{
FileStream fs = File.Create(fullPath);
fs.Close();
Application.logMessageReceived += logCallBack;
}
else
{
Debug.LogError("directory is not exist");
}
}
private void logCallBack(string condition, string stackTrace, LogType type)
{
if (File.Exists(fullPath))
{
using (StreamWriter sw = File.AppendText(fullPath))
{
sw.WriteLine(condition);
sw.WriteLine(stackTrace);
}
}
}
private static Logger s_instance;
public static Logger instance
{
get
{
if (null == s_instance)
s_instance = new Logger();
return s_instance;
}
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持得得之家。如有错误或未考虑完全的地方,望不吝赐教。
织梦狗教程
本文标题为:如何保存Unity中的Log日志


基础教程推荐
猜你喜欢
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C# 解析XML和反序列化的示例 2023-04-14
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C#中的Linq to JSON操作详解 2023-06-08
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- 实例详解C#实现http不同方法的请求 2022-12-26
- Unity shader实现高斯模糊效果 2023-01-16