diff --git a/Assets/Logging/LogManager.cs b/Assets/Logging/LogManager.cs new file mode 100644 index 0000000..3d28f9a --- /dev/null +++ b/Assets/Logging/LogManager.cs @@ -0,0 +1,310 @@ +using System; +using log4net; +using log4net.Core; + +/// +/// 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. +/// +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 + } + +} + +/// +/// Logger which has the ILog interface, but only prints to Unity Console. +/// +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(); + } +} \ No newline at end of file diff --git a/Assets/Logging/LogManager.cs.meta b/Assets/Logging/LogManager.cs.meta new file mode 100644 index 0000000..8b3d4ca --- /dev/null +++ b/Assets/Logging/LogManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eb68c75fd253c85448c34b1409a85083 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Managers/GameManager.cs b/Assets/Scripts/Managers/GameManager.cs index 1f14cf7..a8ea57e 100644 --- a/Assets/Scripts/Managers/GameManager.cs +++ b/Assets/Scripts/Managers/GameManager.cs @@ -7,8 +7,9 @@ using UnityEngine.Events; using UnityEngine.SceneManagement; using FishNet; using System.Collections.Generic; -using log4net; using UnityEditor; +using log4net.Config; +using log4net; /// /// The available scenes in the order they are in the build settings. /// @@ -30,7 +31,7 @@ namespace Managers public class GameManager : MonoBehaviour { - private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); /// /// Globally accessible member to use manager with. @@ -64,7 +65,7 @@ namespace Managers { G = this; if (!IsTestRun) ShowStartScreen(); - ConfigureLog4Net(); + LogManager.ConfigureLogging(); Log.Info("Awake"); } @@ -83,19 +84,6 @@ namespace Managers } } - /// - /// Configuration of log 4 net, before the game starts. - /// - void ConfigureLog4Net() - { - log4net.GlobalContext.Properties["LogFileName"] = $"{Application.dataPath}" + - "\\Logging\\SSOLog"; - var fi = new FileInfo($"{Application.dataPath}" + - "\\Logging\\Log4NetConfiguration.xml"); - XmlConfigurator.Configure(fi); - Log.Debug("Log4Net configured."); - } - /// /// Instantiates the managers needed to play the game. /// diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index f30e7fc..5a628c2 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -141,8 +141,8 @@ PlayerSettings: tvOSBundleVersion: 1.0 bundleVersion: 0.1 preloadedAssets: - - {fileID: -4938997134116425971, guid: 4b98f800e99c70140ac675a637d71d3a, type: 2} - {fileID: 11400000, guid: 7d6dd64b5f2213d4f8cc395ae58ffb43, type: 2} + - {fileID: 7907058041053682329, guid: 4b98f800e99c70140ac675a637d71d3a, type: 2} metroInputSource: 0 wsaTransparentSwapchain: 0 m_HolographicPauseOnTrackingLoss: 1 @@ -658,7 +658,7 @@ PlayerSettings: Stadia: UNITY_POST_PROCESSING_STACK_V2 Standalone: UNITY_POST_PROCESSING_STACK_V2;FISHNET;FISHNET_V4 VisionOS: UNITY_POST_PROCESSING_STACK_V2 - WebGL: UNITY_POST_PROCESSING_STACK_V2 + WebGL: UNITY_POST_PROCESSING_STACK_V2;FISHNET;FISHNET_V4 XboxOne: UNITY_POST_PROCESSING_STACK_V2 tvOS: UNITY_POST_PROCESSING_STACK_V2 additionalCompilerArguments: {}