unity 日志级别_Unity 自定义日志保存!

这篇博客介绍了如何在Unity中实现自定义的日志跟踪,包括开启、关闭跟踪日志,以及设置日志选项。通过创建`DebugTrace`类,可以将日志保存到文件,并在编辑器或非编辑器环境中控制日志的生成。日志信息包含时间戳、日志类型、日志字符串和堆栈轨迹。
摘要由CSDN通过智能技术生成

using UnityEngine;

using System.IO;

using System;

using System.Diagnostics;

using Debug = UnityEngine.Debug;

public class DebugTrace

{

private FileStream fileStream;

private StreamWriter streamWriter;

private bool isEditorCreate = false ; //是否在编辑器中也产生日志文件

private int showFrames = 1000 ; //打印所有

# region instance

private static readonly object obj = new object ;

private static DebugTrace m_instance;

public static DebugTrace Instance

{

get

{

if (m_instance == null )

{

lock (obj)

{

if (m_instance == null )

m_instance = new DebugTrace;

}

}

return m_instance;

}

}

# endregion

private DebugTrace ( )

{

}

///

/// 开启跟踪日志信息

///

public void StartTrace ( )

{

if (Debug.unityLogger.logEnabled)

{

if (Application.isEditor)

{

//在编辑器中设置isEditorCreate==true时候产生日志

if (isEditorCreate)

{

CreateOutlog;

}

}

//不在编辑器中 是否产生日志由 Debug.unityLogger.logEnabled 控制

else

{

CreateOutlog;

}

}

}

private void Application_logMessageReceivedThreaded ( string logString, string stackTrace, LogType type )

{

// Debug.Log(stackTrace); //打包后staackTrace为空 所以要自己实现

if (type != LogType.Warning)

{

// StackTrace stack = new StackTrace(1,true); //跳过第二?(1)帧

StackTrace stack = new StackTrace( true ); //捕获所有帧

string stackStr = string .Empty;

int frameCount = stack.FrameCount; //帧数

if ( this .showFrames > frameCount) this .showFrames = frameCount; //如果帧数大于总帧速 设置一下

//自定义输出帧数,可以自行试试查看效果

for ( int i = stack.FrameCount - this .showFrames; i < stack.FrameCount; i++)

{

StackFrame sf = stack.GetFrame(i); //获取当前帧信息

// 1:第一种 ps:GetFileLineNumber 在发布打包后获取不到

stackStr += "at [" + sf.GetMethod.DeclaringType.FullName +

"." + sf.GetMethod.Name +

".Line:" + sf.GetFileLineNumber + "]n " ;

//或者直接调用tostring 显示数据过多 且打包后有些数据获取不到

// stackStr += sf.ToString;

}

//或者 stackStr = stack.ToString;

string content = string .Format( "time: {0} logType: {1} logString: {2} nstackTrace: {3} {4} " ,

DateTime.Now.ToString( "HH:mm:ss" ), type, logString, stackStr, "rn" );

streamWriter.WriteLine(content);

streamWriter.Flush;

}

}

private void CreateOutlog ( )

{

if (!Directory.Exists(Application.dataPath + "/../" + "OutLog" ))

Directory.CreateDirectory(Application.dataPath + "/../" + "OutLog" );

string path = Application.dataPath + "/../OutLog" + "/" + DateTime.Now.ToString( "yyyyMMddHHmmss" ) + "_log.txt" ;

fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);

streamWriter = new StreamWriter(fileStream);

Application.logMessageReceivedThreaded += Application_logMessageReceivedThreaded;

}

///

/// 关闭跟踪日志信息

///

public void CloseTrace ( )

{

Application.logMessageReceivedThreaded -= Application_logMessageReceivedThreaded;

streamWriter.Dispose;

streamWriter.Close;

fileStream.Dispose;

fileStream.Close;

}

///

/// 设置选项

///

/// 是否记录日志

/// 是否显示所有堆栈帧 默认只显示当前帧 如果设为0 则显示所有帧

/// 过滤 默认log级别以上

/// 是否在编辑器中产生日志记录 默认不需要

public void SetLogOptions ( bool logEnable, int showFrams = 1 , LogType filterLogType = LogType.Log, bool editorCreate = false )

{

Debug.unityLogger.logEnabled = logEnable;

Debug.unityLogger.filterLogType = filterLogType;

isEditorCreate = editorCreate;

this .showFrames = showFrams == 0 ? 1000 : showFrams;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值