3

Hi I am now using log4net for some time. This is my Logger:

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();

    }
}

As you see, this Appender creates logfiles with maxsize 10K and it will also be rolled daily. This Configruation generates logfilenames as follows:

  • 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

BUT The today created logs look like this

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

What I want is that filenames look like this : Log_yyyy-mm-dd_nr.log

So how do I change that?

4

3 回答 3

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"
于 2015-03-05T08:18:45.073 回答
3

我猜您已将 StaticLogFileName 更改为 true:

_roller.StaticLogFileName = true;

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

_roller.StaticLogFileName = false;

[使用 log4net-rollingfileappender 的提示]

于 2015-03-05T08:10:22.487 回答
1

我终于做了以下事情:

        _roller.DatePattern = "yyyy-mm-dd";
        _roller.PreserveLogFileNameExtension = true;
        _roller.File = Path.Combine(FileDirectory,".log");
        _roller.StaticLogFileName = false;

通过这些更改,我的日志文件如下所示:

  • 2015-03-06.log 最后一个
  • 2015-03-06.1.log

谢谢大家的帮助

于 2015-03-06T12:23:21.517 回答