- Home /
IL2CPP Android - behaviour could not be instantiated! problem
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[i] != instance)
Destroy(instances[i].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;
}
}
/// <summary>
/// Configure logging to write to Logs\EventLog.txt and the Unity console output.
/// </summary>
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
}
}
Answer by JoshPeterson · Feb 09, 2017 at 12:48 PM
I suspect that some parts of the log4net.dll assembly are being stripped. The IL2CPP build toolchain always does managed code stripping of assemblies, removing managed code from that that is not called at runtime. The Mono build toolchain does not do this by default, so that may account for the difference.
You can indicate to the build toolchain that you want some (or all) of the log4net.dll assembly to be preserved from stripping using a link.xml file. See this page in the manual for details (note that this page mentions iOS, but the same information applies to Android with IL2CPP).
Well, thank you for your response. Now I am getting named mutex error, so believe that I passed previous the previous error. I found out that it is currently not supported.
Just pasting the error here in case you have an idea. Thanks again for helping out.
02-09 17:45:53.291 7277-7291/? I/Unity: NotSupportedException: C:\Program Files\Unity\Editor\Data\il2cpp\libil2cpp\icalls\mscorlib\System.Threading\$$anonymous$$utex.cpp(25) : Unsupported internal call for IL2CPP:$$anonymous$$utex::Create$$anonymous$$utex_internal - "Named mutexes are not supported"
at UnityEngineInternal.WebRequestUtils.RedirectTo (System.String baseUri, System.String redirectUri) [0x00000] in <filename unknown>:0
at UnityEngine.UI.ObjectPool`1[T].Release (.T element) [0x00000] in <filename unknown>:0
at System.Threading.$$anonymous$$utex..ctor (Boolean initiallyOwned, System.String name) [0x00000] in <filename unknown>:0
at log4net.Appender.RollingFileAppender.ActivateOptions () [0x00000] in <filename unknown>:0
at Log4NetConfig.ConfigureAllLogging () [0x00000] in <filename unknown>:0
at UnityEngine.EventSystems.ExecuteEvents.GetEventHandler[T] (UnityEngine.GameObject root) [0x00000] in <filename unknown>:0
at System.Collections.Generic.HashSet`1[T].GetEnumerator () [0x00000] in <filename unknown>:0
System.Diagnostics.StackTrace:init_frames(Int32, Boolean
Yes, named mutexes don't work well on many platforms, so IL2CPP does not support them. It looks like this is co$$anonymous$$g from the log4net code though, so I'm not sure what to do. $$anonymous$$aybe there is a log4net code path that does not use named mutexes.
I believe that solved the issue. I found the mutex in the source code and changed it with 'lock() {}' then recompiled the dll. It looks like it's working. Thank you very much =)
Your answer
Follow this Question
Related Questions
Unity 5.5 il2cpp android doesn't compile -1 Answers
il2cpp android build error unity 2019.1.6f1 6 Answers
IOS IL2CPP runtime error NullReferenceException 1 Answer
android use il2cpp backend,continuous Socket.BeginReceive calls return uncontinuous content。 0 Answers
How to post process Unity-built dll before il2cpp kicks in? 1 Answer