facebook twitter hatena line email

「Unity/Csharp/Log」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ログの記述方法)
(カスタムログ)
 
(同じ利用者による、間の7版が非表示)
行10: 行10:
  
 
Consoleは、Unityメインメニュー/Window/General/Consoleで、開く。
 
Consoleは、Unityメインメニュー/Window/General/Consoleで、開く。
 +
 +
==Logの負荷==
 +
AnalysisのGC Allocに70Bでて、結構、負荷がかかるので、リリース時は、削除したほうが良いと思う。
 +
 +
==カスタムログ==
 +
例:エラーと、警告だけ表示して、通常ログは表示しないように。
 +
 +
CustomLogHandler.cs
 +
<pre>
 +
using System;
 +
using UnityEngine;
 +
 +
public class CustomLogHandler : ILogHandler
 +
{
 +
    private readonly ILogHandler _logHandler;
 +
 +
    public CustomLogHandler(ILogHandler logHandler)
 +
    {
 +
        _logHandler = logHandler;
 +
    }
 +
 +
    public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
 +
    {
 +
        if (logType != LogType.Error && logType != LogType.Warning) return;
 +
        _logHandler.LogFormat(logType, context, format, args);
 +
    }
 +
 +
    public void LogException(Exception exception, UnityEngine.Object context)
 +
    {
 +
        _logHandler.LogException(exception, context);
 +
    }
 +
}
 +
</pre>
 +
MainScene.cs
 +
<pre>
 +
class MainScene
 +
{
 +
    void Awake()
 +
    {
 +
        Debug.unityLogger.logHandler = new CustomLogHandler(Debug.unityLogger.logHandler);
 +
        Debug.Log("test");
 +
        Debug.LogWarning("war");
 +
        Debug.LogError("err");
 +
 +
        try
 +
        {
 +
            throw new Exception("hoge!");
 +
        }
 +
        catch (Exception e)
 +
        {
 +
            Debug.LogError("message=" + e.Message);
 +
        }
 +
        throw new Exception("hoge exception!");
 +
    }
 +
}
 +
</pre>
 +
LogExceptionメソッドには、キャッチしなかったExceptionの文言が入る。
 +
 +
参考:https://nekojara.city/unity-log-customization

2023年9月28日 (木) 02:46時点における最新版

ログの記述方法

using UnityEngine;
Debug.Log("ログです");
Debug.LogError("エラーログです");
Debug.Log("hoge=" + hoge);
Debug.Log($"hoge={hoge}");

ログは、Consoleに表示される。

Consoleは、Unityメインメニュー/Window/General/Consoleで、開く。

Logの負荷

AnalysisのGC Allocに70Bでて、結構、負荷がかかるので、リリース時は、削除したほうが良いと思う。

カスタムログ

例:エラーと、警告だけ表示して、通常ログは表示しないように。

CustomLogHandler.cs

using System;
using UnityEngine;

public class CustomLogHandler : ILogHandler
{
    private readonly ILogHandler _logHandler;

    public CustomLogHandler(ILogHandler logHandler)
    {
        _logHandler = logHandler;
    }

    public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
    {
        if (logType != LogType.Error && logType != LogType.Warning) return;
        _logHandler.LogFormat(logType, context, format, args);
    }

    public void LogException(Exception exception, UnityEngine.Object context)
    {
        _logHandler.LogException(exception, context);
    }
}

MainScene.cs

class MainScene
{
    void Awake()
    {
        Debug.unityLogger.logHandler = new CustomLogHandler(Debug.unityLogger.logHandler);
        Debug.Log("test");
        Debug.LogWarning("war");
        Debug.LogError("err");

        try
        {
            throw new Exception("hoge!");
        }
        catch (Exception e)
        {
            Debug.LogError("message=" + e.Message);
        }
        throw new Exception("hoge exception!");
    }
}

LogExceptionメソッドには、キャッチしなかったExceptionの文言が入る。

参考:https://nekojara.city/unity-log-customization