Unity3D 记录到日志

前言

记录日志是一个好习惯,方便我们在日常开发中出现问题快速排查。

一、需要引入log4net.dll类库(注意版本)

把它放到Unity Project视图下,最好是建一个“Plugins”文件夹用来存放,在脚本中引用该类库。

下载链接:https://download.csdn.net/download/WenHuiJun_/87658498

二、设置log4net.config文件

原文:https://www.ngui.cc/el/1788499.html?action=onClick

注:对于“ <file type="log4net.Util.PatternString" value="%property{ApplicationLogPath}\\log.log" />“,当是Winform时,不写“type”属性时,“%property{ApplicationLogPath}”改为“Debug”目录下的文件夹名即可(与应用程序在一个目录下),Unity如果不这样设置的话,则会保存到Unity的安装文件路径下。

内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net>
    <logger name="log" additivity="false">    
      <level value="ALL" />
      <appender-ref ref="UnityLog" />
    </logger> 
    <appender name="UnityLog" type="log4net.Appender.RollingFileAppender">
      <!--保存到文件-->     
      <file type="log4net.Util.PatternString" value="%property{ApplicationLogPath}\\log.log" />
      <appendToFile value="true" />   
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1024KB" />     
      <rollingStyle value="Size" />
      <datePattern value="yyyy-MM-dd&quot;.log&quot;" />
      <staticLogFileName value="false" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="【%d [%t] %p %r】-%m%n"/>
      </layout>
    </appender>
  </log4net>
</configuration>
 

三、把log4net.config文件放到工程中(此处放在StreamingAssets文件夹下),编写脚本

using log4net;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public static class UnityMessageLog 
{
    static UnityMessageLog()
    {
        //log4net.config的路径
        string sPath = Application.streamingAssetsPath + "/log4net.config";
        //设置 Properties(log4net.config文件中用到的日志输出路径) 中 ApplicationLogPath 为 Application.streamingAssetsPath路径
        GlobalContext.Properties["ApplicationLogPath"] = Path.Combine(Application.streamingAssetsPath, "log");
        log4net.Config.XmlConfigurator.Configure(new FileInfo(sPath));
    }
    public static ILog GetLogger(string name = "log")
    {
        return LogManager.GetLogger(name);
    }
    /// <summary>
    /// 记录日志,info
    /// </summary>
    /// <param name="message"></param>
    /// <param name="ex"></param>
    public static void Info(string message, Exception ex = null)
    {
        GetLogger().Info(message, ex);
    }
    /// <summary>
    /// 记录日志,Debug
    /// </summary>
    /// <param name="message"></param>
    /// <param name="ex"></param>
    public static void Debug(string message, Exception ex = null)
    {
        ILog pLog = GetLogger();
        pLog.Debug(message, ex);
    }
    /// <summary>
    /// 记录日志,Debug
    /// </summary>
    /// <param name="ex"></param>
    public static void Debug(Exception ex)
    {
        GetLogger().Debug(ex.Message, ex);
    }
    /// <summary>
    /// 记录日志,Error
    /// </summary>
    /// <param name="message"></param>
    /// <param name="ex"></param>
    public static void Error(string message, Exception ex = null)
    {
        GetLogger().Error(message, ex);
    }
    /// <summary>
    /// 记录日志,Error
    /// </summary>
    /// <param name="ex"></param>
    public static void Error(Exception ex)
    {
        GetLogger().Error(ex.Message, ex);
    }
    /// <summary>
    /// 记录日志,Fatal
    /// </summary>
    /// <param name="ex"></param>
    public static void Fatal(Exception ex)
    {
        GetLogger().Fatal(ex.Message, ex);
    }
}

四、调用

    private void Start()
    {
        UnityMessageLog.Debug("this is Debug!");
        UnityMessageLog.Error("this is Error!");
        UnityMessageLog.Info("this is Info11");
    }

五、结果

猜你喜欢

转载自blog.csdn.net/WenHuiJun_/article/details/129988271