【问题标题】:Alternating Logfiles交替日志文件
【发布时间】:2014-04-27 14:33:53
【问题描述】:

我正在尝试用 c# 编写一个 Logfile Writer 类。我想做以下事情:我将日志写入第一个日志文件,如果超过一定大小,它将写入第二个日志文件。如果第二个日志文件已满,则删除第一个日志文件并再次写入该日志文件,依此类推。

到目前为止我已经得到了这个:

public class LogfileBuilder
{
    StreamWriter sW;
    const string file1 = "EventLogging1.txt";
    const string file2 = "EventLogging2.txt";
    FileInfo fInfo;
    public void writeLine(string write)
    {
        string usingFile;
        fInfo = new FileInfo(file1);
        long s1 = fInfo.Length;
        if (s1 > 300)
        {
            fInfo = new FileInfo(file2);
            long s2 = fInfo.Length;
            if (s2 > 300)
            {
                File.Create(file1).Close();
                usingFile = file1;
            }
            else
            {
                usingFile = file2;
            }
        }
        else usingFile = file1;


        using (sW = File.AppendText(usingFile))
        {
            sW.WriteLine(write);
        }
    }
}

谁能帮我完成?

【问题讨论】:

  • 你有没有考虑过使用 log4net 已经内置了这种功能?
  • 到底是什么问题?
  • 问题是,如果我检查第一个是否已满而第二个是否已满,它将删除第一个文件。但是如果第二个文件已满而第一个文件较新怎么办 --> 它仍然会删除第一个文件
  • 只需遍历日志文件并使用长度小于限制的第一个。

标签: c# streamwriter logfiles


【解决方案1】:

我花了一些时间,但(最终)经过一些更改后,您的代码现在可以按要求工作了。

public class LogfileBuilder
{
    StreamWriter sW;
    const string file1 = "EventLogging1.txt";
    const string file2 = "EventLogging2.txt";
    FileInfo fInfo;

    // moved this variable out of function to track which file was used during last writing
    string usingFile = null;                  
    public void writeLine(string write)
    {
        fInfo = new FileInfo(file1);
        long s1 = fInfo.Length;
        if (s1 > 300)
        {
            // added below check to delete and re-create the file#1 (of zero bytes)
            if (usingFile == file1)
                File.Create(file2).Close();

            fInfo = new FileInfo(file2);
            long s2 = fInfo.Length;
            if (s2 > 300)
            {
                File.Create(file1).Close();
                usingFile = file1;
            }
            else
            {
                usingFile = file2;
            }
        }
        else 
            usingFile = file1;

        using (sW = File.AppendText(usingFile))
        {
            sW.WriteLine(write);
        }
    }
}

顺便说一句,你应该研究一下 log4net(正如 martin_costello 所建议的那样),它有很多特性和很大的灵活性。 参考:Apache log4net features list

【讨论】:

    【解决方案2】:

    我想应该是这样的:

    public class LogfileBuilder
    {       
        private const string DEFAULT_PATH_1 = "EventLogging1.txt";
        private const string DEFAULT_PATH_2 = "EventLogging2.txt";
    
        private readonly string _filePath1;
        private readonly string _filePath2;
        private readonly long _delimiterSize;
    
        public LogfileBuilder(long delimiterSize = 300)
        {
            _filePath1 = DEFAULT_PATH_1;
            _filePath2 = DEFAULT_PATH_2;
            _delimiterSize = delimiterSize;
        }
    
        public LogfileBuilder(string filePath1, string filePath2, long delimiterSize = 300)
        {
            _filePath1 = filePath1;
            _filePath2 = filePath2;
            _delimiterSize = delimiterSize;
        }
    
        public void Log(string content)
        {           
            //No file1
            if(File.Exists(_filePath1) == false)
            {
                //No file1, file2
                if(File.Exists(_filePath2) == false)
                {
                    //Creates/overrides file1
                    File.WriteAllText(_filePath1, content);
                }
                //file2, no file1
                else
                {
                    var fileInfo = new FileInfo(_filePath2);
                    //file2 > delimiter
                    if(fileInfo.Length > _delimiterSize)
                    {
                        File.Delete(_filePath2);
                        //Creates/overrides file1
                        File.WriteAllText(_filePath1, content);
                    }
                    //file2 < delimiter
                    else
                    {
                        File.AppendAllText(_filePath2, content);
                    }
                }               
            }
            //file1
            else
            {
                var fileInfo = new FileInfo(_filePath1);
                //file1 > delimiter
                if(fileInfo.Length > _delimiterSize)
                {
                    File.Delete(_filePath1);
                    //Creates/override filepath2
                    File.WriteAllText(_filePath2, content);
                }
                //file1 < delimiter
                else
                {
                    File.AppendAllText(_filePath1, content);    
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 2019-01-12
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 2014-12-28
      • 1970-01-01
      相关资源
      最近更新 更多