unity引入log4net日志框架,保存日志文件到目录
这是找到的最简单的方法,在unity中引入log4net,网上好多方法都很复杂,试了多个均失败,只有这个简单,测试成功。
所有步骤均如文中,并没有在设置窗口中进行其它设置,或者挂载到什么游戏对象之类。
下载
需要引入log4net.dll类库(注意版本) 把它放到Unity Project视图下,最好是建一个“Plugins”文件夹用来存放,在脚本中引用该类库。

设置log4net.config文件
<?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文件夹
Assets/log4net.config也放一份


编写脚本UnityMessageLog.cs
这个脚本并没有往任何物体上挂载,也没有继承其它类
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");
}
查看结果


浙公网安备 33010602011771号