Search code examples
unity-game-enginelog4netlog4net-configuration

Log4net log file not created when unity app installed and running in another system


I have added log4net log file to my unity app. When I run my app in my own machine log files created successfully and log the data but when I publish build of my app and run it in another system, log files are not created. Please suggest any idea what I am missing while created in log file. Here is the code used for create log file.

Sample code :

  public static void ConfigureAllLogging()
    {
        try
        {
            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 = true,
                File = @"TestLog/log.txt",
                Layout = patternLayout,
                MaxSizeRollBackups = 1,
                MaximumFileSize = "10MB",
                RollingStyle = RollingFileAppender.RollingMode.Size,
                StaticLogFileName = true,
                LockingModel = new RollingFileAppender.MinimalLock(),


            };


            fileAppender.ActivateOptions();

            var unityLogger = new UnityAppender
            {
                Layout = new PatternLayout()
            };
            unityLogger.ActivateOptions();

            BasicConfigurator.Configure(fileAppender);
        }
        catch(Exception ex)
        {
            LoggingManager.Error(ex, "ConfigureAllLogging", "Log4netLogHelper");
        }
    }

Solution

  • It's very likely that the issue is due to where you're saving the file. The code will work in the Editor but fail in a build. Unity expects you to save and read data from Application.persistentDataPath. This is the only path you can read and write to in Unity across every platform.

    Replace

    File = @"TestLog/log.txt",
    

    with

    File = Application.persistentDataPath + "/TestLog/log.txt";