这是我的问题,我想通过c#应用程序和log4net登录2个不同的文件(一个用于错误,一个用于普通信息),我在xml中找到了配置,但我想从代码中执行此操作,这是我找到的用于设置一个文件记录的源:
public static void Setup()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date THREAD [%thread] %logger: %message%newline"; //%-5level
patternLayout.ActivateOptions();
RollingFileAppender infoRoller = new RollingFileAppender();
infoRoller.AppendToFile = true;
infoRoller.File = @"logs\";
infoRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
infoRoller.MaxSizeRollBackups = 3;
infoRoller.Layout = patternLayout;
infoRoller.DatePattern = @"IN\FO\_yyyy-MM-dd.\lo\g";
infoRoller.StaticLogFileName = false;
infoRoller.ActivateOptions();
hierarchy.Root.AddAppender(infoRoller);
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
hierarchy.Root.Level = Level.Debug;
hierarchy.Configured = true;
}
在另一个日志记录类中:
private readonly ILog log = LogManager.GetLogger("GENERAL-LOG");
和
Logger.Setup();
log.Debug("Configurazioni caricate con successo.");
代码有效。
现在,我做了这个改变以得到2个日志:
public static void Setup()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date THREAD [%thread] %logger: %message%newline"; //%-5level
patternLayout.ActivateOptions();
RollingFileAppender infoRoller = new RollingFileAppender();
infoRoller.AppendToFile = true;
infoRoller.File = @"logs\";
infoRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
infoRoller.MaxSizeRollBackups = 3;
infoRoller.Layout = patternLayout;
infoRoller.DatePattern = @"IN\FO\_yyyy-MM-dd.\lo\g";
infoRoller.StaticLogFileName = false;
infoRoller.ActivateOptions();
hierarchy.Root.AddAppender(infoRoller);
//ADDED CODE
RollingFileAppender errorRoller = new RollingFileAppender();
errorRoller.AppendToFile = true;
errorRoller.File = @"logs\";
errorRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
errorRoller.MaxSizeRollBackups = 3;
errorRoller.Layout = patternLayout;
errorRoller.DatePattern = @"ERROR_yyyy-MM-dd.\lo\g";
errorRoller.StaticLogFileName = false;
errorRoller.ActivateOptions();
hierarchy.Root.AddAppender(errorRoller);
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
hierarchy.Root.Level = Level.Debug;
hierarchy.Configured = true;
}
}
来自其他班级:
private readonly ILog log = LogManager.GetLogger("GENERAL-LOG");
private readonly ILog elog = LogManager.GetLogger("ERROR-LOG");
Logger.Setup();
log.Debug("prova scrittura in file1");
elog.Error("prova errore scrittura in file2");
结果:两个日志文件都是用两条消息写的。
请提出建议,非常感谢!
答案 0 :(得分:0)
问题是您已将两个appender附加到“root”记录器。如果您想要两个记录器,则必须将它们附加到特定记录器。
在那里,我评论了hierarchy.Root.AddAppender(errorRoller);
和hierarchy.Root.AddAppender(infoRoller);
,因为不再需要它,因为appender被附加到相关的记录器而不是根。
找到下面的代码。在那里,我已经将log和elog作为参数传递给方法,并分别附加相关的appender。
private static void Setup2(ILog infoLog, ILog elog)
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date THREAD [%thread] %logger: %message%newline"; //%-5level
patternLayout.ActivateOptions();
RollingFileAppender infoRoller = new RollingFileAppender();
infoRoller.AppendToFile = true;
infoRoller.File = @"logs\";
infoRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
infoRoller.MaxSizeRollBackups = 3;
infoRoller.Layout = patternLayout;
infoRoller.DatePattern = @"IN\FO\_yyyy-MM-dd.\lo\g";
infoRoller.StaticLogFileName = false;
infoRoller.ActivateOptions();
// hierarchy.Root.AddAppender(infoRoller); //Not appended to the root logger anymore
//ADDED CODE
RollingFileAppender errorRoller = new RollingFileAppender();
errorRoller.AppendToFile = true;
errorRoller.File = @"logs\";
errorRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
errorRoller.MaxSizeRollBackups = 3;
errorRoller.Layout = patternLayout;
errorRoller.DatePattern = @"ERROR_yyyy-MM-dd.\lo\g";
errorRoller.StaticLogFileName = false;
errorRoller.ActivateOptions();
// hierarchy.Root.AddAppender(errorRoller); //Not appended to the root logger anymore
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
hierarchy.Root.Level = Level.Debug;
hierarchy.Configured = true;
ILog log = infoLog;
Logger logger = (Logger)log.Logger;
logger.AddAppender(errorRoller);
log = elog;
logger = (Logger)log.Logger;
logger.AddAppender(infoRoller);
}
调用方法
private readonly ILog log = LogManager.GetLogger("GENERAL-LOG");
private readonly ILog elog = LogManager.GetLogger("ERROR-LOG");
Logger.Setup2(log,elog);
log.Debug("prova scrittura in file1");
elog.Error("prova errore scrittura in file2");
答案 1 :(得分:0)
请用下面的代码块替换Setup()方法相关的代码块。
的变化: 1.删除" hierarchy.Root.AddAppender(infoRoller);"并评论" hierarchy.Root.AddAppender(errorRoller);"。 2.删除" MemoryAppender"似乎在你的情况下不需要。
public static void Setup()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date THREAD [%thread] %logger: %message%newline"; //%-5level
patternLayout.ActivateOptions();
ILog log = LogManager.GetLogger("GENERAL-LOG");
Logger l = (Logger)log.Logger;
RollingFileAppender infoRoller = new RollingFileAppender();
infoRoller.AppendToFile = true;
infoRoller.File = @"logs\";
infoRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
infoRoller.MaxSizeRollBackups = 3;
infoRoller.Layout = patternLayout;
infoRoller.DatePattern = @"IN\FO\_yyyy-MM-dd.\lo\g";
infoRoller.StaticLogFileName = false;
infoRoller.ActivateOptions();
l.AddAppender(infoRoller);
//ADDED CODE
ILog elog = LogManager.GetLogger("ERROR-LOG");
Logger el = (Logger)elog.Logger;
RollingFileAppender errorRoller = new RollingFileAppender();
errorRoller.AppendToFile = true;
errorRoller.File = @"logs\";
errorRoller.RollingStyle = RollingFileAppender.RollingMode.Date;
errorRoller.MaxSizeRollBackups = 3;
errorRoller.Layout = patternLayout;
errorRoller.DatePattern = @"ERROR_yyyy-MM-dd.\lo\g";
errorRoller.StaticLogFileName = false;
errorRoller.ActivateOptions();
el.AddAppender(errorRoller);
//hierarchy.Root.AddAppender(errorRoller);
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
hierarchy.Configured = true;
}
希望这会对你有所帮助。
谢谢