纳金网
标题:
对5.3.3的Debug.logger的理解
[打印本页]
作者:
雅雅
时间:
2016-4-20 00:19
标题:
对5.3.3的Debug.logger的理解
已知public static
ILogger
logger(Debug会自己Get default debug logger给它赋值)
又
logger
Implements interfaces: ILogger, ILogHandle(Initializes a new instance of the Logger)
其中ILogger Implements interfaces:
ILogHandle
(并且ILogger 包含一个ILogHandle属性)
可推测在执行类似Debug.Log的Api时会收到消息事件执行logger的ILogHandle,在ILogHandle中往下执行ILogger。
我们可以自定义继承ILogHandle接口的类替换logger的ILogHandle从而达到对日志的加工处理(如日志存档,发送等)
截取消息事件也可用Application中的委托处理。
官方ILogHandle示列(Interface for custom log handler implementation)
using UnityEngine;
using System.Collections;
using System.IO;
using System;
public class MyFileLogHandler : ILogHandler
{
private FileStream m_FileStream;
private StreamWriter m_StreamWriter;
private ILogHandler m_DefaultLogHandler = Debug.logger.logHandler;
public MyFileLogHandler()
{
string filePath = Application.persistentDataPath + "/MyLogs.txt";
m_FileStream = new FileStream (filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
m_StreamWriter = new StreamWriter (m_FileStream);
// Replace the default debug log handler
Debug.logger.logHandler = this;
}
public void LogFormat (LogType logType, UnityEngine.Object context, string format, params object[] args)
{
m_StreamWriter.WriteLine ( String.Format (format, args) );
m_StreamWriter.Flush ();
m_DefaultLogHandler.LogFormat (logType, context, format, args);
}
public void LogException (Exception exception, UnityEngine.Object context)
{
m_DefaultLogHandler.LogException (exception, context);
}
}
public class MyGameClass : MonoBehaviour {
private static ILogger logger = Debug.logger;
private static string kTAG = "MyGameTag";
private MyFileLogHandler myFileLogHandler;
void Start() {
myFileLogHandler = new MyFileLogHandler();
logger.Log(kTAG, "MyGameClass Start.");
}
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5