csharp 异常记录器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 异常记录器相关的知识,希望对你有一定的参考价值。

using UnityEngine;
using System.Collections;
using System.IO;

public class Logger : MonoBehaviour
{
    public string saveFile = Path.Combine(Application.persistentDataPath, string.Format("Log{0}.txt", DateTime.Now.ToString("yyyyMMddHHmmssffff")));
    private StringWriter logWriter;

    void OnEnable()
    {
        Application.RegisterLogCallback(ExceptionWriter);
    }

    void OnDisable()
    {
        Application.RegisterLogCallback(null);
    }

    void ExceptionWriter(string message, string stackTrace, LogType type)
    {
        switch (type)
        {
            case LogType.Exception:
            case LogType.Error:
                using (StreamWriter writer = new StreamWriter(new FileStream(saveFile, FileMode.Append)))
                {
                    writer.WriteLine(type);
                    writer.WriteLine(message);
                    writer.WriteLine(stackTrace);
                }
                break;
            default:
                break;
        }
    }
}

以上是关于csharp 异常记录器的主要内容,如果未能解决你的问题,请参考以下文章

关于异常Microsoft.CSharp.RuntimeBinder.RuntimeBinderException

csharp C#异常TRY和CATCH

csharp C#使用TRY捕获异常

csharp 无法捕获异步void异常

csharp 数据库异常

csharp 异常日志