GitHub:https://github.com/baishuisr1/Unity-Save-the-log
使用方法:
將Write.cs掛載到GameObject
在Awake或start中調(diào)用Write.console.LogStart();
輸出方法:Write.Log()
截圖.png
輸出TXT,默認地址為/StreamingAssets/Log文件夾下(注:文件保存只會在打包后輸出,IDE中運行不會保存文件)
Weite.Log()輸出格式:時間-調(diào)用類-調(diào)用方法-輸出內(nèi)容
默認輸出格式:時間-信息類型-相關(guān)代碼所在地址-內(nèi)容
截圖.png
源代碼:
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>
/// 開始寫入日志,參數(shù)一:是否寫入Warning類型數(shù)據(jù),默認不寫入,參數(shù)二:是否寫入Debug.Log類型數(shù)據(jù),默認不寫入,參數(shù)三:是否寫入全部數(shù)據(jù),默認不寫入,參數(shù)四:是否將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; //獲取當(dāng)前場景運行環(huán)境
_WarningDisplay = WarningDisplay;
_LogDisplay = LogDisplay;
_AllDisplay = AllDisplay;
_LogData = LogData;
if (IsIDE == false) //判斷當(dāng)前場景運行環(huán)境,如果是Editor中則不執(zhí)行
{
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");
//設(shè)置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(); //獲取調(diào)用類信息
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("請檢測是否調(diào)用了Console.LogStart方法,或者關(guān)閉控制臺Log寫入與所有數(shù)據(jù)寫入項");
}
}
}
else
{
Debug.Log("請檢測是否調(diào)用了Console.LogStart方法,或者關(guān)閉控制臺Log寫入與所有數(shù)據(jù)寫入項");
}
}
private void LogCallback(string condition, string stackTrace, LogType type) //寫入控制臺數(shù)據(jù)
{
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() //關(guān)閉寫入
{
if ((FileWriter != null) && (IsIDE == false))
{
FileWriter.Close();
Application.RegisterLogCallback(null);
}
}
}