First add a link to the log4net dll in the Unity editor. You can do this by placing the dll log4net in the Assets/Plugins directory or in the Assets/Plugins/ child directory.
Once log4net referenced, now you need to configure your applications so that log4net knows how to actually start logging. This is what I have for my configuration:
/// <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 = @"Logs\EventLog.txt", Layout = patternLayout, MaxSizeRollBackups = 5, MaximumFileSize = "1GB", RollingStyle = RollingFileAppender.RollingMode.Size, StaticLogFileName = true }; fileAppender.ActivateOptions(); var unityLogger = new UnityAppender { Layout = new PatternLayout() }; unityLogger.ActivateOptions(); BasicConfigurator.Configure(unityLogger, fileAppender); }
This sets log4net for logging in Logs\EventLog.txt and for logging the console console through the UnityAppender class. The UnityAppender class looks like this (I have it as a private inner class):
/// <summary> An appender which logs to the unity console. </summary> private class UnityAppender : AppenderSkeleton { /// <inheritdoc /> protected override void Append(LoggingEvent loggingEvent) { string message = RenderLoggingEvent(loggingEvent); if (Level.Compare(loggingEvent.Level, Level.Error) >= 0) { // everything above or equal to error is an error Debug.LogError(message); } else if (Level.Compare(loggingEvent.Level, Level.Warn) >= 0) { // everything that is a warning up to error is logged as warning Debug.LogWarning(message); } else { // everything else we'll just log normally Debug.Log(message); } } }
Then be sure to call ConfigureAllLogging() somewhere where you know it will be called. I installed it in the static constructors of one of my global MonoBehavoirs.
source share