Legacy Documentation: Version 5.6 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

ILogHandler

interface in UnityEngine

Description

Interface for custom log handler implementation.

ILogHandler interface to ease unit-testing and mocking of loggers.

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."); } }

Public Functions

LogExceptionA variant of ILogHandler.LogFormat that logs an exception message.
LogFormatLogs a formatted message.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961