using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SaveLog : MonoBehaviour
{
public bool isSavaLog = true;
public bool isSaveError = true;
private void Awake()
{
Application.logMessageReceived += onHandleLog;
string fileDif = Application.persistentDataPath + "/Debug/Error/";
Debug.Log("日记保存路径:" + fileDif);
}
private void onHandleLog(string condition, string stackTrace, LogType type)
{
string time = DateTime.Now.ToString("yyy-MM-dd HH:mm");
LogInfo info = new LogInfo();
info.condition = condition;
info.stackTrace = stackTrace;
info.type = type;
info.tiem = time;
info.deviceUniqueIdentifier = SystemInfo.deviceUniqueIdentifier;
info.deviceName = SystemInfo.deviceName;
info.operatingSystem = SystemInfo.operatingSystem;
info.systemMemorySize = SystemInfo.systemMemorySize;
info.deviceModel = SystemInfo.deviceModel;
string jsonStr = JsonUtility.ToJson(info);
if (type == LogType.Error || type == LogType.Exception)
{
string fileDif = Application.persistentDataPath + "/Debug/Error/";
if (isSaveError) writeText(fileDif, jsonStr);
}
else
{
string fileDif = Application.persistentDataPath + "/Debug/Log/";
if (isSavaLog) writeText(fileDif, jsonStr);
}
}
private void writeText(string fileDif, string message)
{
//string fileDif = Application.persistentDataPath + "/Debug/Error/";
if (!Directory.Exists(fileDif))
{
Directory.CreateDirectory(fileDif);
}
string textPath = fileDif + DateTime.Now.ToString("yyy-MM-dd") + ".txt";
if (string.IsNullOrEmpty(textPath) || string.IsNullOrEmpty(message)) return;
StreamWriter sw = null;
if (!File.Exists(textPath)) sw = File.CreateText(textPath);
else sw = File.AppendText(textPath);
sw.WriteLine(message + '\n');
sw.Close();
sw.Dispose();
}
}
[Serializable]
public class LogInfo
{
/// <summary>日志ID</summary>
public string id;
/// <summary>计数</summary>
public int count = 1;
/// <summary>日志条件</summary>
public string condition;
/// <summary>堆栈跟踪信息</summary>
public string stackTrace;
/// <summary>日志类型</summary>
public LogType type;
/// <summary>时间</summary>
public string time;
/// <summary>设备唯一标识符</summary>
public string deviceUniqueIdentifier;
/// <summary>设备名称</summary>
public string deviceName;
/// <summary>操作系统</summary>
public string operatingSystem;
/// <summary>系统内存大小</summary>
public int systemMemorySize;
/// <summary>设备型号</summary>
public string deviceModel;
/// <summary>日记记录时间</summary>
public string tiem;
}
文章来源地址https://www.toymoban.com/news/detail-741270.html
文章来源:https://www.toymoban.com/news/detail-741270.html
到了这里,关于Unity保存日记到本地的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!