我正在用C#编写Powershell模块作为DLL。为了存储我的libs的所有配置,我创建了一个配置对象singleton。这个想法是不使用任何XML文件,因为app.config等不适用于DLL,我不想发送带有单独XML文件的DLL。
其中一个配置主题是日志记录。所以我在代码中完全配置它。这工作正常,但我不能让NHibernate写一行代码!我如何唤醒NHIbernate的记录器?
这是我在配置文件中设置的内容(路径仅用于调试):
public sealed class LibConfig
{
public static readonly LibConfig instance = new LibConfig();
... // other members
static LibConfig()
{
}
private LibConfig()
{
ConfigLogger();
... //other stuff
}
public static LibConfig Instance => instance;
public void ConfigLogger()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
var LoggerNH = (Logger)hierarchy.GetLogger("NHibernate.SQL");
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
RollingFileAppender roller = new RollingFileAppender();
roller.File = @"D:\temp\BedInPersistanceLog.txt";
roller.AppendToFile = true;
roller.MaximumFileSize = "50MB";
roller.MaxSizeRollBackups = 5;
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.LockingModel = new FileAppender.MinimalLock();
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
roller.MaximumFileSize = "1GB";
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.StaticLogFileName = true;
roller.ActivateOptions();
hierarchy.Root.Level = Level.Debug;
hierarchy.Root.AddAppender(roller);
RollingFileAppender rollerNH = new RollingFileAppender();
rollerNH.AppendToFile = false;
rollerNH.File = @"D:\temp\NHibernateLog.txt";
rollerNH.AppendToFile = true;
rollerNH.MaximumFileSize = "50MB";
rollerNH.MaxSizeRollBackups = 5;
rollerNH.RollingStyle = RollingFileAppender.RollingMode.Size;
rollerNH.LockingModel = new FileAppender.MinimalLock();
rollerNH.Layout = patternLayout;
rollerNH.MaxSizeRollBackups = 5;
rollerNH.MaximumFileSize = "1GB";
rollerNH.RollingStyle = RollingFileAppender.RollingMode.Size;
rollerNH.StaticLogFileName = true;
rollerNH.ActivateOptions();
LoggerNH.Level = Level.Debug;
LoggerNH.AddAppender(rollerNH);
hierarchy.Configured = true;
}
}
在我的库的类中,我然后使用:
public class MyClass
{
private static readonly ILog log = logManager.GetLogger(typeof(Operations));
.....
public void AnyMethod()
{
log.DEBUG("Some message");
}
}
这很好但NHibernate的日志文件总是0 Bytes = empty
。 如何激励Log4Net / NHibernate将其输出写入该文件?我缺少什么?
谢谢!