在静态上下文中使用时,Log4net 在 DLL 库中不起作用
Log4net doesn't work from DLL library when using in static context
我在图书馆项目中有一个名为 "Utils" 的 class。我编译库项目并将 .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");
}
当 运行 时,我没有错误消息并且目录已创建,但是没有日志,甚至 "Initializing Library successful..." 也没有。
我的问题似乎经常发生在处理您无权访问的 exe 文件时(例如,允许您直接在文本框中编写 C# 代码的应用程序,然后自编译并且您无权访问编译、调试或 运行 过程)。
经过大量调试后,我决定转向工作完美的 NLog,并且在 [此处] 阅读了一些评论后,我相信 NLog 无论如何都比 Log4Net 好。 1
我在图书馆项目中有一个名为 "Utils" 的 class。我编译库项目并将 .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");
}
当 运行 时,我没有错误消息并且目录已创建,但是没有日志,甚至 "Initializing Library successful..." 也没有。
我的问题似乎经常发生在处理您无权访问的 exe 文件时(例如,允许您直接在文本框中编写 C# 代码的应用程序,然后自编译并且您无权访问编译、调试或 运行 过程)。
经过大量调试后,我决定转向工作完美的 NLog,并且在 [此处] 阅读了一些评论后,我相信 NLog 无论如何都比 Log4Net 好。 1