94 lines
2.7 KiB
C#
94 lines
2.7 KiB
C#
using System;
|
|
using System.IO;
|
|
using NLog;
|
|
using NLog.Config;
|
|
using NLog.Targets;
|
|
using UnityEngine;
|
|
|
|
public static class __Log
|
|
{
|
|
public delegate void Print(string msg);
|
|
private static Print _print;
|
|
|
|
private static FileLog _fileLog;
|
|
|
|
static __Log()
|
|
{
|
|
_fileLog = new FileLog();
|
|
}
|
|
public static void SetOutput(Print print)
|
|
{
|
|
_print -= print;
|
|
_print += print;
|
|
}
|
|
public static void Info(string str)
|
|
{
|
|
var msg = $"[Info]:{str}";
|
|
_fileLog.Info(msg);
|
|
_print?.Invoke(msg);
|
|
}
|
|
public static void Error(string str)
|
|
{
|
|
var msg = $"[color=#ff0000][Error]:{str}[/color]";
|
|
_fileLog.Error(msg);
|
|
_print?.Invoke(msg);
|
|
}
|
|
public static void InfoFile(string str)
|
|
{
|
|
var msg = $"[Info]:{str}";
|
|
_fileLog.Info(msg);
|
|
}
|
|
public static void ErrorFile(string str)
|
|
{
|
|
var msg = $"[color=#ff0000][Error]:{str}[/color]";
|
|
_fileLog.Error(msg);
|
|
}
|
|
}
|
|
|
|
public class FileLog
|
|
{
|
|
private NLog.ILogger _logger;
|
|
|
|
public FileLog()
|
|
{
|
|
#if !UNITY_EDITOR && UNITY_ANDROID
|
|
LoggingConfiguration config = new LoggingConfiguration();
|
|
|
|
FileTarget fileTarget = new FileTarget();
|
|
config.AddTarget("MNRTarget", fileTarget);
|
|
fileTarget.FileName = Path.Combine(Application.persistentDataPath, $"Logs/{DateTime.Now.ToLocalTime(): yyyy-MM-dd HH:mm:ss}.log");
|
|
fileTarget.ArchiveFileName = Path.Combine(Application.persistentDataPath, $"Logs/{{#}}.log");
|
|
fileTarget.Layout =
|
|
@"${newline:when=level>=LogLevel.Error}[${level}] ${time} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=3} ${message} ${stacktrace:format=Raw:topFrames=20:skipFrames=3:separator='\r\n':reverse=true:when=level>=LogLevel.Error}";
|
|
fileTarget.DeleteOldFileOnStartup = true;
|
|
fileTarget.ArchiveNumbering = ArchiveNumberingMode.Date;
|
|
fileTarget.ArchiveEvery = FileArchivePeriod.Day;
|
|
fileTarget.ArchiveDateFormat = "yyyyMMddHH";
|
|
fileTarget.KeepFileOpen = true;
|
|
fileTarget.CreateDirs = true;
|
|
|
|
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, fileTarget));
|
|
|
|
LogManager.Configuration = config;
|
|
_logger = LogManager.GetLogger("MNRLog");
|
|
#endif
|
|
}
|
|
|
|
public void Info(string str)
|
|
{
|
|
#if !UNITY_EDITOR && UNITY_ANDROID
|
|
this._logger.Info(str);
|
|
#else
|
|
UnityEngine.Debug.Log(str);
|
|
#endif
|
|
}
|
|
|
|
public void Error(string str)
|
|
{
|
|
#if !UNITY_EDITOR && UNITY_ANDROID
|
|
this._logger.Error(str);
|
|
#else
|
|
UnityEngine.Debug.LogError(str);
|
|
#endif
|
|
}
|
|
} |