log4net 设置两个文件的日志记录与 c# 代码的不同级别日志记录
log4net set logging of two files with a different level logging from c# code
这是我的问题,我想通过 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;
}
在另一个 class 中用于记录:
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;
}
}
来自其他 class:
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");
结果:两个日志文件都写入了两条消息。
请多多指教,万分感谢!
问题是您已将两个附加程序附加到 "root" 记录器。如果你想有两个记录器,那么你必须将它们附加到特定的记录器。
我已经评论了 hierarchy.Root.AddAppender(errorRoller);
和 hierarchy.Root.AddAppender(infoRoller);
因为不再需要它,因为附加程序附加到相关记录器而不是根。
找到下面的代码。在那里我将 log 和 elog 作为参数传递给方法并分别附加相关的附加程序。
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");
请将与 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;
}
希望对您有所帮助。
谢谢
这是我的问题,我想通过 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;
}
在另一个 class 中用于记录:
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;
}
}
来自其他 class:
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");
结果:两个日志文件都写入了两条消息。
请多多指教,万分感谢!
问题是您已将两个附加程序附加到 "root" 记录器。如果你想有两个记录器,那么你必须将它们附加到特定的记录器。
我已经评论了 hierarchy.Root.AddAppender(errorRoller);
和 hierarchy.Root.AddAppender(infoRoller);
因为不再需要它,因为附加程序附加到相关记录器而不是根。
找到下面的代码。在那里我将 log 和 elog 作为参数传递给方法并分别附加相关的附加程序。
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");
请将与 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;
}
希望对您有所帮助。
谢谢