前言
记录日志是一个好习惯,方便我们在日常开发中出现问题快速排查。
一、需要引入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的安装文件路径下。文章来源:https://www.toymoban.com/news/detail-811986.html
内容如下:文章来源地址https://www.toymoban.com/news/detail-811986.html
<?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".log"" />
<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");
}
五、结果
到了这里,关于Unity3D 记录到日志的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!