Unity Log保存到本地

GitHub:https://github.com/baishuisr1/Unity-Save-the-log

使用方法:
將Write.cs掛載到GameObject
在Awake或start中調用Write.console.LogStart();
輸出方法:Write.Log()

 

截圖.png

 

輸出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;

}

}

void Start()

{

LogStart();

}

/// <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; //獲取當前場景運行環境

IsIDE = false;

_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.ToString("yyyy-MM-dd HH:mm:ss:fff") + " " + "[" + 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 = "";

if (_AllDisplay == false)

{

if (type.ToString() == "Warning")

if (_WarningDisplay == false)

{

condition = "";

stackTrace = "";

content = "";

}

else

{

content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " " + "[" + type + "]" + "[" + stackTrace + "]" + " " + ":" + " " +

condition +

Environment.NewLine;

}

 

if (type.ToString() == "Log")

if (_LogDisplay == false)

{

condition = "";

stackTrace = "";

content = "";

}

else

{

content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " " + "[" + type + "]" + "[" + stackTrace + "]" + " " + ":" + " " +

condition +

Environment.NewLine;

}

if (type.ToString() == "Exception")

content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " " + "[" + type + "]" + "[" + stackTrace + "]" + " " + ":" + " " + condition +

Environment.NewLine;

}

else

{

content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " " + "[" + 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);

}

}

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章