Hello everyone,
In my android application, I am using log4net.dll for logging and I am using il2cpp scripting backend. However, when I run the application logger does not work and in the android logs I see that
The script behaviour 'Log4NetConfig' could not be instantiated!
System.Diagnostics.StackTrace:init_frames(Int32, Boolean)
UnityEngine.StackTraceUtility:ExtractStackTrace()
System.Collections.Generic.HashSet`1:GetEnumerator()
I don’t know how to debug this. When I use mono it works fine. But it does not work with IL2CPP. log4net.dll is a managed dll.
I appreciate any help.
And this is the script
using UnityEngine;
using System.Collections;
using log4net.Layout;
using log4net.Appender;
using log4net.Config;
public class Log4NetConfig : MonoBehaviour
{
static Log4NetConfig instance;
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); //
void Awake()
{
if (instance == null)
{
instance = this;
}
Log4NetConfig[] instances = GameObject.FindObjectsOfType<Log4NetConfig>();
if (instances.Length > 1)
{
for (int i = 0; i < instances.Length; i++)
{
if (instances *!= instance)*
Destroy(instances*.gameObject);*
}
}
DontDestroyOnLoad(this);
ConfigureAllLogging();
}
void OnEnable()
{
Application.logMessageReceivedThreaded += instance.OnApplicationLog;
}
void OnDisable()
{
Application.logMessageReceivedThreaded -= instance.OnApplicationLog;
}
void OnApplicationLog(string logString, string stackTrace, LogType type)
{
switch (type)
{
case LogType.Warning:
log.Warn(logString + " — " + stackTrace);
break;
case LogType.Assert:
log.Fatal(logString + " — " + stackTrace);
break;
case LogType.Error:
case LogType.Exception:
log.Error(logString + " — " + stackTrace);
break;
case LogType.Log:
log.Debug(logString + " — " + stackTrace);
break;
}
}
///
/// Configure logging to write to Logs\EventLog.txt and the Unity console output.
///
public static void ConfigureAllLogging()
{
var patternLayout = new PatternLayout
{
ConversionPattern = “%date %-5level %logger - %message%newline”
};
patternLayout.ActivateOptions();
// setup the appender that writes to Log\EventLog.txt
var fileAppender = new RollingFileAppender
{
AppendToFile = false,
File = Application.persistentDataPath + “/EventLog.txt”,
Layout = patternLayout,
MaxSizeRollBackups = 3,
MaximumFileSize = “10MB”,
RollingStyle = RollingFileAppender.RollingMode.Size,
StaticLogFileName = true
};
#if UNITY_EDITOR
var unityLogger = new UnityAppender
{
Layout = new PatternLayout()
};
unityLogger.ActivateOptions();
fileAppender.Threshold = log4net.Core.Level.All;
fileAppender.ActivateOptions();
BasicConfigurator.Configure(unityLogger, fileAppender);
#else
fileAppender.Threshold = log4net.Core.Level.All;
fileAppender.ActivateOptions();
BasicConfigurator.Configure(fileAppender);
#endif
}
}