This wrapper essentially restricts the logger to the unity console, when using WebGL, while I don't need to change any Reference to LogManager or Log.Debug(etc) in my code.
310 lines
6.6 KiB
C#
310 lines
6.6 KiB
C#
using System;
|
|
using log4net;
|
|
using log4net.Core;
|
|
|
|
/// <summary>
|
|
/// Wraps Log4Net to only be used when not bein in a WebGL environment.
|
|
/// When the game runs in WebGL, only the default console logs are used.
|
|
/// </summary>
|
|
public static class LogManager
|
|
{
|
|
public static Level GlobalLogLevel = Level.Debug;
|
|
|
|
public static log4net.ILog GetLogger(Type type)
|
|
{
|
|
#if !UNITY_WEBGL
|
|
return log4net.LogManager.GetLogger(type);
|
|
#else
|
|
return new ConsoleLogger();
|
|
#endif
|
|
}
|
|
|
|
public static void ConfigureLogging()
|
|
{
|
|
#if !UNITY_WEBGL
|
|
log4net.GlobalContext.Properties["LogFileName"] = $"{Application.dataPath}" +
|
|
"\\Logging\\SSOLog";
|
|
var fi = new FileInfo($"{Application.dataPath}" +
|
|
"\\Logging\\Log4NetConfiguration.xml");
|
|
XmlConfigurator.Configure(fi);
|
|
Log.Debug("Log4Net configured.");
|
|
#else
|
|
UnityEngine.Debug.Log("When using WebGL, logging is restricted to the console and unity's built in logger.");
|
|
#endif
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logger which has the ILog interface, but only prints to Unity Console.
|
|
/// </summary>
|
|
public class ConsoleLogger : log4net.ILog
|
|
{
|
|
public bool IsDebugEnabled
|
|
{
|
|
get
|
|
{
|
|
return LogManager.GlobalLogLevel >= Level.Debug;
|
|
}
|
|
}
|
|
|
|
|
|
public bool IsInfoEnabled
|
|
{
|
|
get
|
|
{
|
|
return LogManager.GlobalLogLevel >= Level.Info;
|
|
}
|
|
}
|
|
|
|
|
|
public bool IsWarnEnabled
|
|
{
|
|
get
|
|
{
|
|
return LogManager.GlobalLogLevel >= Level.Warn;
|
|
}
|
|
}
|
|
|
|
|
|
public bool IsErrorEnabled
|
|
{
|
|
get
|
|
{
|
|
return LogManager.GlobalLogLevel >= Level.Error;
|
|
}
|
|
}
|
|
|
|
|
|
public bool IsFatalEnabled
|
|
{
|
|
get
|
|
{
|
|
return LogManager.GlobalLogLevel >= Level.Error;
|
|
}
|
|
}
|
|
|
|
|
|
log4net.Core.ILogger ILoggerWrapper.Logger => throw new NotImplementedException();
|
|
|
|
/* Log a message object */
|
|
public void Debug(object message)
|
|
{
|
|
if (IsDebugEnabled)
|
|
UnityEngine.Debug.Log(message);
|
|
}
|
|
|
|
|
|
public void Info(object message)
|
|
{
|
|
if (IsInfoEnabled)
|
|
UnityEngine.Debug.Log(message);
|
|
}
|
|
|
|
|
|
public void Warn(object message)
|
|
{
|
|
if (IsWarnEnabled)
|
|
UnityEngine.Debug.LogWarning(message);
|
|
}
|
|
|
|
|
|
public void Error(object message)
|
|
{
|
|
if (IsErrorEnabled)
|
|
UnityEngine.Debug.LogError(message);
|
|
}
|
|
|
|
|
|
public void Fatal(object message)
|
|
{
|
|
if (IsFatalEnabled)
|
|
UnityEngine.Debug.LogError(message);
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Log a message object and exception */
|
|
public void Debug(object message, Exception t)
|
|
{
|
|
if (IsDebugEnabled)
|
|
UnityEngine.Debug.Log(string.Format("{0}\n{1}: {2}\n{3}", message, t.GetType().ToString(), t.Message, t.StackTrace));
|
|
}
|
|
|
|
|
|
public void Info(object message, Exception t)
|
|
{
|
|
if (IsInfoEnabled)
|
|
UnityEngine.Debug.Log(string.Format("{0}\n{1}: {2}\n{3}", message, t.GetType().ToString(), t.Message, t.StackTrace));
|
|
}
|
|
|
|
|
|
public void Warn(object message, Exception t)
|
|
{
|
|
if (IsWarnEnabled)
|
|
UnityEngine.Debug.LogWarning(string.Format("{0}\n{1}: {2}\n{3}", message, t.GetType().ToString(), t.Message, t.StackTrace));
|
|
}
|
|
|
|
|
|
public void Error(object message, Exception t)
|
|
{
|
|
if (IsErrorEnabled)
|
|
UnityEngine.Debug.LogError(string.Format("{0}\n{1}: {2}\n{3}", message, t.GetType().ToString(), t.Message, t.StackTrace));
|
|
}
|
|
|
|
|
|
public void Fatal(object message, Exception t)
|
|
{
|
|
if (IsFatalEnabled)
|
|
UnityEngine.Debug.LogError(string.Format("{0}\n{1}: {2}\n{3}", message, t.GetType().ToString(), t.Message, t.StackTrace));
|
|
}
|
|
|
|
|
|
/* Log a message string using the System.String.Format syntax */
|
|
public void DebugFormat(string format, params object[] args)
|
|
{
|
|
if (IsDebugEnabled)
|
|
UnityEngine.Debug.Log(string.Format(format, args));
|
|
}
|
|
|
|
|
|
public void InfoFormat(string format, params object[] args)
|
|
{
|
|
if (IsInfoEnabled)
|
|
UnityEngine.Debug.Log(string.Format(format, args));
|
|
}
|
|
|
|
|
|
public void WarnFormat(string format, params object[] args)
|
|
{
|
|
if (IsWarnEnabled)
|
|
UnityEngine.Debug.LogWarning(string.Format(format, args));
|
|
}
|
|
|
|
|
|
public void ErrorFormat(string format, params object[] args)
|
|
{
|
|
if (IsErrorEnabled)
|
|
UnityEngine.Debug.LogError(string.Format(format, args));
|
|
}
|
|
|
|
|
|
public void FatalFormat(string format, params object[] args)
|
|
{
|
|
if (IsFatalEnabled)
|
|
UnityEngine.Debug.LogError(string.Format(format, args));
|
|
}
|
|
|
|
|
|
|
|
/* Log a message string using the System.String.Format syntax */
|
|
public void DebugFormat(IFormatProvider provider, string format, params object[] args)
|
|
{
|
|
if (IsDebugEnabled)
|
|
UnityEngine.Debug.Log(string.Format(provider, format, args));
|
|
}
|
|
|
|
|
|
public void InfoFormat(IFormatProvider provider, string format, params object[] args)
|
|
{
|
|
if (IsInfoEnabled)
|
|
UnityEngine.Debug.Log(string.Format(provider, format, args));
|
|
}
|
|
|
|
|
|
public void WarnFormat(IFormatProvider provider, string format, params object[] args)
|
|
{
|
|
if (IsWarnEnabled)
|
|
UnityEngine.Debug.LogWarning(string.Format(provider, format, args));
|
|
}
|
|
|
|
|
|
public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
|
|
{
|
|
if (IsErrorEnabled)
|
|
UnityEngine.Debug.LogError(string.Format(provider, format, args));
|
|
}
|
|
|
|
|
|
public void FatalFormat(IFormatProvider provider, string format, params object[] args)
|
|
{
|
|
if (IsFatalEnabled)
|
|
UnityEngine.Debug.LogError(string.Format(provider, format, args));
|
|
}
|
|
|
|
public void DebugFormat(string format, object arg0)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void DebugFormat(string format, object arg0, object arg1)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void DebugFormat(string format, object arg0, object arg1, object arg2)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void InfoFormat(string format, object arg0)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void InfoFormat(string format, object arg0, object arg1)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void InfoFormat(string format, object arg0, object arg1, object arg2)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void WarnFormat(string format, object arg0)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void WarnFormat(string format, object arg0, object arg1)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void WarnFormat(string format, object arg0, object arg1, object arg2)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void ErrorFormat(string format, object arg0)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void ErrorFormat(string format, object arg0, object arg1)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void ErrorFormat(string format, object arg0, object arg1, object arg2)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void FatalFormat(string format, object arg0)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void FatalFormat(string format, object arg0, object arg1)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void FatalFormat(string format, object arg0, object arg1, object arg2)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
} |