Unity5.4 保存日志记录TXT文件到本地
来源:互联网 发布:淘宝的达摩盘 编辑:程序博客网 时间:2024/04/28 03:56
GitHub:https://github.com/baishuisr1/Unity-Save-the-log
使用方法:
将Write.cs挂载到GameObject
在Awake或start中调用Write.console.LogStart();
输出方法:Write.Log()
输出TXT,默认地址为/StreamingAssets/Log文件夹下(注:文件保存只会在打包后输出,IDE中运行不会保存文件)
Weite.Log()输出格式:时间-调用类-调用方法-输出内容
默认输出格式:时间-信息类型-相关代码所在地址-内容
源代码:
using System;using System.Diagnostics;using System.IO;using System.Text;using UnityEngine;using Debug = UnityEngine.Debug;public class Write : MonoBehaviour{ private static FileStream FileWriter; private static UTF8Encoding encoding; private static Write _consoleLog; private static bool _AllDisplay; private static bool _LogDisplay; private static bool _WarningDisplay; private static bool _LogData; private static bool IsIDE; private FileInfo fileInfo; private string NowTime; public static Write console //开启单例 { get { if (_consoleLog == null) _consoleLog = new Write(); return _consoleLog; } } /// <summary> /// 开始写入日志,参数一:是否写入Warning类型数据,默认不写入,参数二:是否写入Debug.Log类型数据,默认不写入,参数三:是否写入全部数据,默认不写入,参数四:是否将Log方法信息输出到控制台,默认输出 /// </summary> /// <param name="WarningDisplay"></param> public void LogStart(bool WarningDisplay = false, bool LogDisplay = false, bool AllDisplay = false, bool LogData = true) { if ((FileWriter == null)) { IsIDE = Application.isEditor; //获取当前场景运行环境 _WarningDisplay = WarningDisplay; _LogDisplay = LogDisplay; _AllDisplay = AllDisplay; _LogData = LogData; if (IsIDE == false) //判断当前场景运行环境,如果是Editor中则不执行 { Directory.CreateDirectory(Application.dataPath + "/StreamingAssets"); Directory.CreateDirectory(Application.dataPath + "/StreamingAssets/" + "Log"); NowTime = DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_"); fileInfo = new FileInfo(Application.dataPath + "/StreamingAssets/Log/" + NowTime + "_Log.txt"); //设置Log文件输出地址 FileWriter = fileInfo.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); encoding = new UTF8Encoding(); Application.logMessageReceived += LogCallback; } } } /// <summary> /// 替代Debug.log写入Log信息 /// </summary> /// <param name="_log"></param> /// <param name="con"></param> public static void Log(object _log) { if ((_LogDisplay == false) && (_AllDisplay == false)) { if (_LogData) Debug.Log(_log); if (IsIDE == false) { try { var trace = new StackTrace(); //获取调用类信息 var ClassName = trace.GetFrame(1).GetMethod().DeclaringType.Name; var WayName = trace.GetFrame(1).GetMethod().Name; var log = DateTime.Now + " " + "[" + ClassName + "." + WayName + "]" + " " + ":" + " " + _log + Environment.NewLine; FileWriter.Write(encoding.GetBytes(log), 0, encoding.GetByteCount(log)); } catch (Exception) { Debug.Log("请检测是否调用了Console.LogStart方法,或者关闭控制台Log写入与所有数据写入项"); } } } else { Debug.Log("请检测是否调用了Console.LogStart方法,或者关闭控制台Log写入与所有数据写入项"); } } private void LogCallback(string condition, string stackTrace, LogType type) //写入控制台数据 { string content = null; if (_AllDisplay == false) { if (type.ToString() == "Warning") if (_WarningDisplay == false) { condition = ""; stackTrace = ""; content = ""; } else { content = DateTime.Now + " " + "[" + type + "]" + "[" + stackTrace + "]" + " " + ":" + " " + condition + Environment.NewLine; } if (type.ToString() == "Log") if (_LogDisplay == false) { condition = ""; stackTrace = ""; content = ""; } else { content = DateTime.Now + " " + "[" + type + "]" + "[" + stackTrace + "]" + " " + ":" + " " + condition + Environment.NewLine; } if (type.ToString() == "Exception") content = DateTime.Now + " " + "[" + type + "]" + "[" + stackTrace + "]" + " " + ":" + " " + condition + Environment.NewLine; } else { content = DateTime.Now + " " + "[" + type + "]" + "[" + stackTrace + "]" + " " + ":" + " " + condition + Environment.NewLine; } FileWriter.Write(encoding.GetBytes(content), 0, encoding.GetByteCount(content)); FileWriter.Flush(); } private void OnDestroy() //关闭写入 { if ((FileWriter != null) && (IsIDE == false)) { FileWriter.Close(); Application.RegisterLogCallback(null); } }}
阅读全文
0 0
- Unity5.4 保存日志记录TXT文件到本地
- 将dataTable 保存到本地txt文件中
- 【C#】将文本保存到本地txt文件中
- 保存文件到本地
- log4j保存日志到本地
- txt文件记录错误日志
- Android 日志输出到本地txt
- Android 日志输出到本地txt
- 爬取网站中的邮箱保存到本地txt文件中
- 记录日志到文件
- JS保存文件到本地
- java保存日志到本地文本文件
- android:Log日志保存到本地
- android logger 把日志保存到本地
- Android 日志保存到本地---CscSDK
- php 下载保存文件保存到本地
- android 日志保存到文件
- 日志文件保存到Document
- Python appium selenium当页面元素 没有加载出来时 ,等待元素加载出来的方法
- 多线程互斥锁访问算法(上)------Peterson算法
- MySQL数据库读写分离
- 使用Java模拟数据库排序集合对象
- 二叉树的基本定义和遍历
- Unity5.4 保存日志记录TXT文件到本地
- 【IOI2000第五题】邮局问题
- 闲聊历史上的配角之赵高
- 初识springboot
- mysql优化2
- NLP 专题论文解读:从 Chatbot 到 NER | PaperDaily #11
- 表面缺陷检测的几种方法
- 数据类型对应字节数(32位,64位 int 占字节数)
- CG插画入门教程之人体动态基础与面部画法分析