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...".