Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question log4net broken in Unity 2021.1.13f1+

Discussion in 'Editor & General Support' started by Wing-Dancer, Jan 4, 2022.

  1. Wing-Dancer

    Wing-Dancer

    Joined:
    Feb 27, 2019
    Posts:
    1
    Hi,

    I have been struggling with this for the entire day, maybe someone else can confirm or tell me what I'm doing wrong. Maybe suggest an alternative?

    I wanted to use log4net for rolling log files that I could check in preproduction mode for a game I'm making. Unfortunately, log4net doesn't seem to feel too well - it stops logging in arbitrary spots, at worst logging only twice in an Update of a MonoBehaviour.

    I have this:

    Code (CSharp):
    1. using log4net;
    2. using log4net.Appender;
    3. using log4net.Config;
    4. using log4net.Core;
    5. using log4net.Layout;
    6. using System;
    7. using System.Collections;
    8. using System.Collections.Concurrent;
    9. using System.Collections.Generic;
    10. using System.IO;
    11. using System.Reflection;
    12. using System.Runtime.CompilerServices;
    13. using UnityEngine;
    14.  
    15.  
    16. public class LazyLogger : MonoBehaviour
    17. {
    18.     private static ILog log;
    19.  
    20.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    21.     public static void ConfigureAllLogging()
    22.     {
    23.         var patternLayout = new PatternLayout
    24.         {
    25.             ConversionPattern = "%date %-5level %logger - %message%newline"
    26.         };
    27.         patternLayout.ActivateOptions();
    28.  
    29.         // setup the appender that writes to Log\EventLog.txt
    30.         var fileAppender = new RollingFileAppender
    31.         {
    32.             AppendToFile = false,
    33.             File = @"Logs\EventLog.txt",
    34.             Layout = patternLayout,
    35.             MaxSizeRollBackups = 5,
    36.             MaximumFileSize = "1GB",
    37.             RollingStyle = RollingFileAppender.RollingMode.Size,
    38.             StaticLogFileName = true
    39.         };
    40.         fileAppender.ActivateOptions();
    41.  
    42.         var unityLogger = new UnityAppender
    43.         {
    44.             Layout = new PatternLayout()
    45.         };
    46.         unityLogger.ActivateOptions();
    47.  
    48.         BasicConfigurator.Configure(unityLogger);
    49.         log = LogManager.GetLogger(typeof(LazyLogger));
    50.     }
    51.  
    52.  
    53.     private class UnityAppender : AppenderSkeleton
    54.     {
    55.         /// <inheritdoc />
    56.         protected override void Append(LoggingEvent loggingEvent)
    57.         {
    58.             string message = RenderLoggingEvent(loggingEvent);
    59.  
    60.             if (Level.Compare(loggingEvent.Level, Level.Error) >= 0)
    61.             {
    62.                 // everything above or equal to error is an error
    63.                 Debug.LogError(message);
    64.             }
    65.             else if (Level.Compare(loggingEvent.Level, Level.Warn) >= 0)
    66.             {
    67.                 // everything that is a warning up to error is logged as warning
    68.                 Debug.LogWarning(message);
    69.             }
    70.             else
    71.             {
    72.                 // everything else we'll just log normally
    73.                 Debug.Log(message);
    74.             }
    75.         }
    76.     }
    77.  
    78.  
    79.     private class UnityDebugAppender : AppenderSkeleton
    80.     {
    81.         protected override void Append(LoggingEvent loggingEvent)
    82.         {
    83.             Debug.Log(RenderLoggingEvent(loggingEvent));
    84.         }
    85.     }
    86.  
    87.     public void Update()
    88.     {
    89.    
    90.  
    91.         log.Info("Logging Info");
    92.         log.Debug("Logging Debug");
    93.         Debug.Log("I'm still going");
    94.      
    95.     }
    96.  
    97.  
    98. }
    Most of the code is taken from an online tutorial. It was behaving weirdly, so I put it into an empty project to run and see if there are any problems just logging. The Update loop only logs twice in versions higher than 2021.1.13f1 . On that version, it works without faults, including the fileAppender I later attached.

    I took the log4net version for NET 4.0, altho I tried almost all of the available ones on the official page. I tried both loading from XML as well as variations on BasicConfiguration. I was not able to get the internal debugging running, so I can't say for sure what is the actual problem.

    Any clues? Maybe I should use something else?

    EDIT: In release, upon building, the logger doesn't work anyway. Back to square one, it seems:

    NotSupportedException: Specified method is not supported.
    at System.Threading.Mutex..ctor (System.Boolean initiallyOwned, System.String name) [0x00006] in <a1e9f114a6e64f4eacb529fc802ec93d>:0
    at (wrapper remoting-invoke-with-check) System.Threading.Mutex..ctor(bool,string)
    at log4net.Appender.RollingFileAppender.ActivateOptions () [0x00135] in <b615f366d2544c2fbd95846ac52f06e4>:0
    at LazyLogger.ConfigureAllLogging () [0x00056] in <342f1c5937504ca9b28be2698b206a16>:0
     
    Last edited: Jan 4, 2022
  2. hemoryi

    hemoryi

    Joined:
    Mar 31, 2022
    Posts:
    4
    I also encountered the same problem.
    It can still be used on the unity version of 2021.1.0f1, but it cannot be used after upgrading to the version of 2021.3.1f1. Only the first few logs can be printed, and no longer printed after that.
     
  3. Florian-Nouviale

    Florian-Nouviale

    Joined:
    Mar 14, 2013
    Posts:
    51
    I have discovered that Unity's version control package ships with its own log4net library, could this be a confilct due to this ?