我有一个名为" Utils"在图书馆项目中。我编译库项目并将.dll分发给第三方开发人员,然后第三方开发人员可以在自己的库中使用它。第三方开发人员调用Utils.InitializeLibrary()以确保.dll中的所有组件都已正确初始化。
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");
}
运行时,我没有错误消息并且创建了目录,但是没有日志,甚至没有"初始化库成功..."。
答案 0 :(得分:0)
似乎我的问题经常发生在处理你无法访问的exe时(例如,允许你直接在文本框中编写C#代码的应用程序,然后自编译,你无法访问编译,调试或运行过程)。
经过大量的调试后,我决定转移到NLog,它运行得很好并且阅读了一些评论[这里],我相信NLog比Log4Net更好。 1