Unity3D 记录到日志

41 篇文章 2 订阅
本文介绍了如何在Unity项目中集成并配置log4net库来实现日志记录。首先,需要将log4net.dll库放入项目,并创建log4net.config配置文件。配置文件详细说明了日志保存的位置和格式。接着,将配置文件放入StreamingAssets文件夹,并在代码中设置日志输出路径。最后,提供了一个静态类用于方便地调用各种日志级别进行记录。
摘要由CSDN通过智能技术生成

前言

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

一、需要引入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");
    }

五、结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值