无法以编程方式配置 log4net Logger
Can't configure log4net Logger programmatically
我需要以编程方式配置 log4net Logger
。具体来说,我需要添加一个 Logger
和一个 appender。然而,当我检索它时,我发现它没有正确的级别或附加程序,希望我错过了一些简单的配置仪式:
var patternLayout = new PatternLayout { ConversionPattern = "%message%newline" };
patternLayout.ActivateOptions();
var hierarchy = (Hierarchy)LogManager.GetRepository();
var testAppender = new RollingFileAppender
{
Name = "test-appender",
AppendToFile = true,
File = "c:\temp\test.log",
Layout = patternLayout,
DatePattern = "yyyyMMdd-HHmm",
MaxSizeRollBackups = 50,
MaximumFileSize = "20MB",
RollingStyle = RollingFileAppender.RollingMode.Size,
StaticLogFileName = false,
Encoding = Encoding.UTF8,
PreserveLogFileNameExtension = true,
Threshold = Level.Debug
};
testAppender.ActivateOptions();
var testLogger = hierarchy.LoggerFactory.CreateLogger(hierarchy, "test");
testLogger.Hierarchy = hierarchy;
testLogger.AddAppender(testAppender);
testLogger.Repository.Configured = true;
testLogger.Level = Level.Debug;
testLogger.Additivity = false;
hierarchy.Configured = true;
var testLogger = LogManager.GetLogger("test");
记录器 'testLogger' 的级别为空且没有附加程序。我错过了什么?
而不是调用
var testLogger = hierarchy.LoggerFactory.CreateLogger(hierarchy, "test");
我打电话
var testLogger = hierarchy.GetLogger("test", hierarchy.LoggerFactory);
这奏效了。如果记录器不存在,GetLogger 将创建它。
我需要以编程方式配置 log4net Logger
。具体来说,我需要添加一个 Logger
和一个 appender。然而,当我检索它时,我发现它没有正确的级别或附加程序,希望我错过了一些简单的配置仪式:
var patternLayout = new PatternLayout { ConversionPattern = "%message%newline" };
patternLayout.ActivateOptions();
var hierarchy = (Hierarchy)LogManager.GetRepository();
var testAppender = new RollingFileAppender
{
Name = "test-appender",
AppendToFile = true,
File = "c:\temp\test.log",
Layout = patternLayout,
DatePattern = "yyyyMMdd-HHmm",
MaxSizeRollBackups = 50,
MaximumFileSize = "20MB",
RollingStyle = RollingFileAppender.RollingMode.Size,
StaticLogFileName = false,
Encoding = Encoding.UTF8,
PreserveLogFileNameExtension = true,
Threshold = Level.Debug
};
testAppender.ActivateOptions();
var testLogger = hierarchy.LoggerFactory.CreateLogger(hierarchy, "test");
testLogger.Hierarchy = hierarchy;
testLogger.AddAppender(testAppender);
testLogger.Repository.Configured = true;
testLogger.Level = Level.Debug;
testLogger.Additivity = false;
hierarchy.Configured = true;
var testLogger = LogManager.GetLogger("test");
记录器 'testLogger' 的级别为空且没有附加程序。我错过了什么?
而不是调用
var testLogger = hierarchy.LoggerFactory.CreateLogger(hierarchy, "test");
我打电话
var testLogger = hierarchy.GetLogger("test", hierarchy.LoggerFactory);
这奏效了。如果记录器不存在,GetLogger 将创建它。