1、配置文件
<?xml version="1.0" encoding="utf-8" ?>
<LogConfigInfo>
<!--LOg 日志核心参数-->
<!--日志路径-->
<LogPath>F:\DoungesonFighterLog.txt</LogPath>
<!--日志的状态:开发模式 Develop/指定代码输出模式 Speacial/部署模式 Deploy/停止模式 Stop-->
<LogState>Develop</LogState>
<!--日志最大容量-->
<LogMaxCapacity>2000</LogMaxCapacity>
<!--日志缓存数量-->
<LogBufferNumber>4</LogBufferNumber>
</LogConfigInfo>2、自定义异常类
using System;
public class XmlAnlysisException : Exception
{
public XmlAnlysisException() : base() { }
public XmlAnlysisException(string exceptionMessage) : base(exceptionMessage) { }
}3、加载文件 接口定义
using UnityEngine;
using System.Collections.Generic;
using System;
public interface IConfigMgr
{
//应用设置
Dictionary<string, string> AppSetting { get; } //得到AppSetting的数量
int GetAppSettingMaxNumber();
}4、加载配置文件
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Linq;
using System.IO;public class LogConfigMgr : IConfigMgr
{
private static Dictionary<string, string> _AppSetting;//定义应用设置集合
public Dictionary<string, string> AppSetting
{
get
{
return _AppSetting;
}
} public LogConfigMgr(string logPath,string xmlRootNodeName)
{
_AppSetting = new Dictionary<string, string>();
InitAddAnalysisXml(logPath, xmlRootNodeName);
}
//初始化解析XMl数据,到集合中
private void InitAddAnalysisXml(string logPath,string xmlRootNodeName)
{
if (string.IsNullOrEmpty(logPath)||string.IsNullOrEmpty(xmlRootNodeName))
{
Debug.LogError("logPath is null or xmlRootNodeName is null");
return;
}
XDocument xmlDoc;
System.Xml.XmlReader xmlReader;
try
{
xmlDoc = XDocument.Load(logPath);
xmlReader = System.Xml.XmlReader.Create(new StringReader(xmlDoc.ToString()));
}
catch
{
throw new XmlAnlysisException(GetType() + "/InitAddAnalysisXml()/XMLb Analysis Exception");
}
//循环解析Xml
while (xmlReader.Read())
{
//Xml读写器从指定根节点开始读写
if (xmlReader.IsStartElement() && xmlReader.LocalName == xmlRootNodeName)
{
//离开using的这个范围后读写器自动释放资源
using (System.Xml.XmlReader xmlReaderItem =xmlReader.ReadSubtree())
{
while (xmlReaderItem.Read())
{
//如果是"节点元素"
if (xmlReaderItem.NodeType == XmlNodeType.Element)
{
//节点元素
string strNode = xmlReaderItem.Name;
//读Xml当前行的下一个内容
xmlReaderItem.Read();
//如果是"节点内容"
if (xmlReaderItem.NodeType == XmlNodeType.Text)
{
//当前行 键值对赋值
_AppSetting[strNode] = xmlReaderItem.Value;
}
}
}
}
}
} }
public int GetAppSettingMaxNumber()
{
if (_AppSetting != null && _AppSetting.Count >= 1)
return _AppSetting.Count;
else
return 0;
}
}5、日志打印
/****************************************************
文件:Log.cs
功能:日志调试,对外接口,方便开发人员调试系统程序,基本实现原理如下
1、把开发人员在代码中定义的调试语句,写入日志的缓存
2、当缓存中的数量超过定义的最大值时写入文件,即把缓存内容调试语句一次性写入文本文件
*****************************************************/using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;//多线程命名空间
public static class Log
{
private static List<string> logInfoArrayList; //Log日志缓存数据
private static string logPath; //日志文件路径
private static State logState; //Log日志状态(部署模式)
private static int logmaxCamacityNumber; //Log日志最大容量(文本中存入的日志的最大容量,大于此容量后,删除多余的)
private static int logMaxBufferNumber; //最缓存容量(用于判断最大缓存多少条时,开始写入文件) static Log()
{
//日志缓存数据
logInfoArrayList = new List<string>();
#if UNITY_STANDALONE_WIN
//日志文件管理器
IConfigMgr configMgr = new LogConfigMgr(KernalConstant.LogConfigInfoPath, KernalConstant.LogConfigInfoRootNodename);
logPath = configMgr.AppSetting["LogPath"];
//日志状态
string strLogState = configMgr.AppSetting["LogState"];
//日志最大容量
string strLogMaxCapacity = configMgr.AppSetting["LogMaxCapacity"];
//日志缓存最大容量
string strLogBufferMaxCapacity = configMgr.AppSetting["LogBufferNumber"];
#endif
if (string.IsNullOrEmpty(logPath))
logPath = UnityEngine.Application.persistentDataPath + "\\DungeonFighterLog.txt"; //日志状态
if (!string.IsNullOrEmpty(strLogState))
{
switch (strLogState)
{
case "Develop":
logState = State.Develop;
break;
case "Speacial":
logState = State.Speacial;
break;
case "Deploy":
logState = State.Deploy;
break;
case "Stop":
logState = State.Stop;
break;
default:
logState = State.Stop;
break;
}
}
else logState = State.Stop;
//日志最大容量
if (!string.IsNullOrEmpty(strLogMaxCapacity))
logmaxCamacityNumber = Convert.ToInt32(strLogMaxCapacity);
else logmaxCamacityNumber = 2000;
//日志缓存最大容量
if (!string.IsNullOrEmpty(strLogMaxCapacity))
logMaxBufferNumber = Convert.ToInt32(strLogBufferMaxCapacity);
else
logMaxBufferNumber = 1;
Debug.Log("....... " + logPath); //创建文件
if (!File.Exists(logPath))//不存在指定路径的文件
{
using (File.Create(logPath))
{
Thread.CurrentThread.Abort();//终止当前线程
}
}
//把日志文件中的数据同步到日志缓存中
SyncFileDataToLogInfoArray();
}//log end private static void SyncFileDataToLogInfoArray()
{
if (!string.IsNullOrEmpty(logPath))
{
StreamReader sr = new StreamReader(logPath);
while (sr.Peek() >= 0)
{
logInfoArrayList.Add(sr.ReadLine());
}
sr.Close();
}
} //写数据到文件
public static void Write(string writeFileDate, Level level)
{
//参数检查
if (logState == State.Stop) return; //如果日志缓存数量超过指定容量则清空
if (logInfoArrayList.Count >= logmaxCamacityNumber)
logInfoArrayList.Clear(); if (!string.IsNullOrEmpty(writeFileDate))
{
//增加日期与时间
writeFileDate = "Log State:" + logState.ToString() + "/" + DateTime.Now.ToString()+"/"+writeFileDate;
//对于不同的"日志状态" 分特定情形写入文件
if(level== Level.High)
writeFileDate = "Important!!!" + writeFileDate;
switch (logState)
{
case State.Develop://开发状态
//追加调试信息,写入文件
AppendDateToFile(writeFileDate);
break;
case State.Speacial://指定状态
if (level == Level.High || level == Level.Speacial)
AppendDateToFile(writeFileDate);
break;
case State.Deploy: //部署状态
if (level == Level.High)
AppendDateToFile(writeFileDate);
break;
case State.Stop://停止状态
break;
}
}
}//write end public static void Write(string writeFileDate)
{
Write(writeFileDate, Level.Low);
} //查询日志缓存中所有数据
public static List<string> QueryAllDateFromLogBuffer()
{
if (logInfoArrayList != null)
return logInfoArrayList;
else
return null;
}
//清出实体日志文件与日志缓存中所有数据
public static void ClearLogFileAndBufferAllDate()
{
if (logInfoArrayList != null)
logInfoArrayList.Clear();
//缓存中是空的,所以相当于同步空文件到实体文件中了,等价于清空实体文件
SyncLogArrayToFile();
} //追击数据到文件
private static void AppendDateToFile(string _writeFileDate)
{
if (!string.IsNullOrEmpty(_writeFileDate))
{
//调试信息数据追加到缓存集合中
logInfoArrayList.Add(_writeFileDate);
}
//缓存集合数量超过一定指定数量(logMaxBufferNumber) 则同步到实体文件中
if (logInfoArrayList.Count % logMaxBufferNumber == 0)
{
//同步缓存中的数据信息到实体文件中
SyncLogArrayToFile();
}
} //同步缓存数据到实体文件中
private static void SyncLogArrayToFile()
{
if (!string.IsNullOrEmpty(logPath))
{
StreamWriter sw = new StreamWriter(logPath);
foreach (string item in logInfoArrayList)
{
sw.WriteLine(item);
}
sw.Close();
}
}}
#region 本类的枚举类型
public enum State
{
Develop, //开发模式,输出所有类容
Speacial, //指定输出模式,指定输出内容
Deploy, //部署模式,只输出最核心日志信息,例如严重错误信息
Stop //停止输出模式,不输出任何日志文件
}public enum Level//调试信息的等级(表示调试信息本身的重要程度)
{
High,
Speacial,
Low
}
#endregion
unity android release包怎么看日志 unity日志系统
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Unity解析key不确定的Json
Unity解析key不确定的Json
JSON Unity -
unity android 闪退 日志 unity报错日志
游戏上线测试,会有很多错误日志生成,也不可能一个个去点过来,因为错误信息是根据日期和账号来区分的,便于管理。所以就需要来处理下,其实很简单,就是把后台PHP存储的txt格式的报错信息写入到一个CSV文件中。 其实这功能我之前就有写过,那时候是在Unity上写了个工具,所以用的是C#,最近为
unity android 闪退 日志 Unity 错误日志 List #include -
androidstudio保存日志 android studio日志怎么看
1.启动Android Studio 2.View->Tool Windows->Terminal 3.在底部输入monitor 4.Android Devices Monitor新窗口打开 5.添加指定app参数
androidstudio保存日志 Android Windows 新窗口