fix: instead of using the log4uni wrapper, I wrote my own

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.
This commit is contained in:
Jakob Feldmann 2024-04-20 16:26:28 +02:00
parent 969b0df42b
commit a3fa41eae2
4 changed files with 327 additions and 18 deletions

View File

@ -0,0 +1,310 @@
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();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: eb68c75fd253c85448c34b1409a85083
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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;
/// <summary>
/// The available scenes in the order they are in the build settings.
/// </summary>
@ -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);
/// <summary>
/// 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
}
}
/// <summary>
/// Configuration of log 4 net, before the game starts.
/// </summary>
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.");
}
/// <summary>
/// Instantiates the managers needed to play the game.
/// </summary>

View File

@ -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: {}