核心代码:
public static event LogCallback logMessageReceivedThreaded;
public static event LogCallback logMessageReceived;
------------------------------------------------------------------------------------------
public delegate void LogCallback(string condition, string stackTrace, LogType type);
完整代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class Logger : MonoBehavIoUr
{
private string outPath;
private void Start()
{
InitLogger();
Debug.Log("11111111111111111111111");
Debug.LogWarning("22222222222222222222222");
Debug.LogError("33333333333333333333333");
}
private void OnEnable()
{
Application.logMessageReceivedThreaded += LogCallBack;
}
private void Ondisable()
{
Application.logMessageReceivedThreaded -= LogCallBack;
}
public void InitLogger()
{
outPath = Application.dataPath + "/outLog.txt";
if (File.Exists(outPath))
{
File.Delete(outPath);
}
Debug.Log(outPath.Replace("/outLog.txt", ""));
if (Directory.Exists(Application.dataPath))
{
FileStream fs = File.Create(outPath);
fs.Close();
Debug.Log("注册回调");
}
else
{
Debug.LogError("文件不存在");
}
}
private void LogCallBack(string condition, string stackTrace, LogType type)
{
Debug.Log("回调执行");
if (File.Exists(outPath))
{
using (StreamWriter sw = File.AppendText(outPath))
{
sw.WriteLine(condition);
sw.WriteLine(stackTrace);
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。