gpt4 book ai didi

c# - 在 RollingFileAppender 文件的文件名中添加日期

转载 作者:行者123 更新时间:2023-11-30 14:09:31 29 4
gpt4 key购买 nike

您好,我现在使用 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 创建了最大大小为 10K 的日志文件,并且它也会每天滚动。此配置生成日志文件名如下:

  • Log.log_2015-03-04,

  • Log.1.log_2015-03-04,

  • Log.2.log_2015-03-04,
  • Log.log_2015-03-03,
  • Log.1.log_2015-03-03,
  • Log.2.log_2015-03-03

但是 今天创建的日志是这样的

  • Log.log,
  • Log.1.log,
  • Log.2.log

我想要的是文件名如下所示:Log_yyyy-mm-dd_nr.log

那么我该如何改变呢?

最佳答案

我猜您已将 StaticLogFileName 更改为 true:

_roller.StaticLogFileName = true;

当使用日期滚动样式时,它应该是 false:

_roller.StaticLogFileName = false;

[tips-on-using-log4net-rollingfileappender]

关于c# - 在 RollingFileAppender 文件的文件名中添加日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28872780/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com