0

I have a class named "Utils" in a library project. I compile the library project and distribute the .dll to a third party developer who can then use it in his own library. The third party developer calls Utils.InitializeLibrary() to make sure that all the components in the .dll are initialized correctly.

public static void InitializeLibrary()
        {


            string sLogPath = Path.Combine(XFuturesLibrary.Properties.Settings.Default.LogFolder, "XLibrary - " + (DateTime.Now).ToString("yyyyMMdd HHmmss")).ToString();
            //log4net.GlobalContext.Properties["LogFileName"] = sLogPath;
            if (!Directory.Exists(sLogPath)) Directory.CreateDirectory(sLogPath);


            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            PatternLayout patternLayout = new PatternLayout();
            patternLayout.ConversionPattern = "%date [%thread] %-5level %logger [%property{NDC}] - %message%newline";
            patternLayout.ActivateOptions();

            // All messages
            RollingFileAppender rollerAll = new RollingFileAppender();
            rollerAll.AppendToFile = true;
            rollerAll.File = sLogPath + "\\log.txt";
            rollerAll.Layout = patternLayout;
            rollerAll.MaxSizeRollBackups = 20;
            rollerAll.MaximumFileSize = "1GB";
            rollerAll.RollingStyle = RollingFileAppender.RollingMode.Size;
            rollerAll.StaticLogFileName = true;
            rollerAll.ActivateOptions();
            hierarchy.Root.AddAppender(rollerAll);

            // Only Warn and Error messages
            RollingFileAppender rollerWarning = new RollingFileAppender();
            rollerWarning.AppendToFile = true;
            rollerWarning.File = sLogPath + "\\errors.txt";
            rollerWarning.Layout = patternLayout;
            rollerWarning.MaxSizeRollBackups = 20;
            rollerWarning.MaximumFileSize = "1GB";
            rollerWarning.RollingStyle = RollingFileAppender.RollingMode.Size;
            rollerWarning.StaticLogFileName = true;
            rollerWarning.Threshold = log4net.Core.Level.Warn;
            rollerWarning.ActivateOptions();
            hierarchy.Root.AddAppender(rollerWarning);

            // Console output
            ConsoleAppender consoleAll = new ConsoleAppender();
            consoleAll.Layout = patternLayout;
            consoleAll.Threshold = log4net.Core.Level.Info;
            consoleAll.ActivateOptions();
            hierarchy.Root.AddAppender(consoleAll);

            hierarchy.Configured = true;

            logger.Info("Initialize Library has been successfully called");
        }

When running, I have no error message and the directory is created, but there's no log, not even the "Initializing Library successful...".

TheWalkingPanda
  • 183
  • 3
  • 9
  • Have you tried adding a breakpoint to the code and stepping through each line? – Lews Therin May 27 '16 at 12:43
  • Does it work if you host the dll in your own application? What sort of application is the client writing (winforms, asp.net, etc)? Is the client also using log4net? – David J May 27 '16 at 12:45
  • It completely works if its called from my own application. I can't add a breakpoint because this only happen when the program is distributed as a .dll. The whole .dll library work, except the logging part. – TheWalkingPanda May 27 '16 at 12:49
  • I just tried to use NLog and I have the same problem. So it's probably more of a library-related problem than Log4Net. – TheWalkingPanda May 30 '16 at 09:58

1 Answers1

0

It seems my problem occurs often when dealing with exe that you don't have access to (e.g. apps that allow you to write C# code directly in a textbox, which is then self compiled and you have no access over the compiling, debugging or running process).

After a lot of debugging, I decided to move to NLog which worked perfectly and having read a few reviews [here], I believe NLog is better than Log4Net anyway. 1

Community
  • 1
  • 1
TheWalkingPanda
  • 183
  • 3
  • 9