您好我现在正在使用log4net一段时间。这是我的记录器:
public static class LoggerSetup
{
private static RollingFileAppender _roller;
private static PatternLayout _patternLayout;
static LoggerSetup()
{
// root config
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.Level = Level.Debug;
hierarchy.Configured = true;
// prepare appender
_roller = new RollingFileAppender();
_patternLayout = new PatternLayout();
_roller.Layout = _patternLayout;
hierarchy.Root.AddAppender(_roller);
_fileDirectory = "Log";
Setup();
}
/// <summary>
/// Location to save logfile
/// </summary>
public static string FileDirectory
{
get
{
return _fileDirectory;
}
set
{
_fileDirectory = value;
Setup();
}
}
private static string _fileDirectory;
public static string DatePattern
{
get { return "_yyyy-MM-dd"; }
}
public static void Setup()
{
_roller.AppendToFile = true;
_roller.File = FileDirectory + @"/" + "Log.log";
_roller.StaticLogFileName = true;
_roller.PreserveLogFileNameExtension = true;
_roller.LockingModel = new FileAppender.MinimalLock();
_roller.MaxSizeRollBackups = -1; //--> Logger doesnt delete files himself
_roller.RollingStyle = RollingFileAppender.RollingMode.Composite;
_roller.MaximumFileSize = "10KB";
_roller.DatePattern = DatePattern;
_patternLayout.ConversionPattern = "%date [%-5level] %message%newline";
_patternLayout.ActivateOptions();
_roller.ActivateOptions();
}
}
如您所见,这个Appender创建了maxsize 10K的日志文件,它也将每天滚动。此Configruation生成日志文件名,如下所示:
Log.log_2015-03-04 ,
Log.1.log_2015-03-04 ,
但是今天创建的日志看起来像这样
我想要的是文件名如下所示: Log_yyyy-mm-dd_nr.log
那我该如何改变呢?
答案 0 :(得分:3)
我猜你已经将StaticLogFileName更改为true:
_roller.StaticLogFileName = true;
使用日期滚动样式时,它应该是假的:
_roller.StaticLogFileName = false;
答案 1 :(得分:3)
你应该将StaticLogFileName设置为false,将DatePattern设置为_yyyy-MM-dd.log
,将File设置为直到日期模式的路径,例如Path.Combine(FileDirectory,"Log_")
:
_roller.File = Path.Combine(FileDirectory,"Log_");
_roller.StaticLogFileName = false;
_roller.DatePattern="_yyyy-MM-dd.log"
答案 2 :(得分:1)
我终于完成了以下工作:
_roller.DatePattern = "yyyy-mm-dd";
_roller.PreserveLogFileNameExtension = true;
_roller.File = Path.Combine(FileDirectory,".log");
_roller.StaticLogFileName = false;
通过这些更改,我的日志文件如下所示:
感谢大家的帮助