1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #region 模块信息
- #endregion
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- using UnityEngine;
- using Debug = UnityEngine.Debug;
- public class WriteLog : MonoBehaviour
- {
- private static FileStream FileWriter;
- private static UTF8Encoding encoding;
- private static WriteLog consoleLog;
- public static WriteLog ConsoleLog
- {
- get
- {
- if (consoleLog == null)
- consoleLog = new WriteLog();
- return consoleLog;
- }
- }
- private void Start()
- {
- #if UNITY_EDITOR
- LogStart();
- #endif
- }
- public void LogStart()
- {
-
- Directory.CreateDirectory(Application.persistentDataPath + "/Log");
- string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace("/", "_").Replace(":", "_");
- FileInfo fileInfo = new FileInfo(Application.persistentDataPath + "/Log/" + NowTime + "_Log.txt");
-
- FileWriter = fileInfo.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
- encoding = new UTF8Encoding();
- Application.logMessageReceived += LogCallback;
- Debug.LogError("开始写入日志");
- }
-
-
-
-
-
-
- private void LogCallback(string condition, string stackTrace, LogType type)
- {
-
- string content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + "【" + type + "】" + "【" + stackTrace + "】" + ":" + condition + Environment.NewLine + Environment.NewLine;
- FileWriter.Write(encoding.GetBytes(content), 0, encoding.GetByteCount(content));
- FileWriter.Flush();
- }
- private void OnDestroy()
- {
-
- }
- private void OnApplicationQuit()
- {
- #if UNITY_EDITOR
-
- if ((FileWriter != null))
- {
- Debug.LogError("日志写入结束");
- FileWriter.Close();
- Application.logMessageReceived -= LogCallback;
- }
- #endif
- }
- }
|