我正在按照这个示例以编程方式配置 log4net。但是,由于我想分别记录信息和错误,所以我修改了设置方法如下
public static void Setup()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
RollingFileAppender info = new RollingFileAppender();
info.AppendToFile = true;
info.File = @"c:\Info\infolog.log";
info.Layout = patternLayout;
info.MaxSizeRollBackups = 5;
info.MaximumFileSize = "1GB";
info.RollingStyle = RollingFileAppender.RollingMode.Size;
info.StaticLogFileName = true;
info.ActivateOptions();
info.Name = "info";
RollingFileAppender error = new RollingFileAppender();
error.AppendToFile = true;
error.File = @"c:\Error\errorlog.log" ;
error.Layout = patternLayout;
error.MaxSizeRollBackups = 5;
error.MaximumFileSize = "1GB";
error.RollingStyle = RollingFileAppender.RollingMode.Size;
error.StaticLogFileName = true;
error.ActivateOptions();
error.Name = "error";
hierarchy.Root.AddAppender(info);
hierarchy.Root.AddAppender(error);
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
hierarchy.Root.Level = Level.Info;
hierarchy.Configured = true;
}
在我的代码中,我将其初始化如下
public class myclass
{
ILog errorLog ;
ILog infoLog ;
public myclass()
{
errorLog = LogManager.GetLogger("error");
infoLog = LogManager.GetLogger("info");
}
public void LogMessage()
{
infoLog.Warn("warn message");
errorLog.Error("Error message");
}
}
问题是我打印到 infoLog 的任何内容都会打印到 infolog.log 文件和 errorlog.log 文件,反之亦然。我的配置有什么问题?