Question

I am trying to write a Logfile Writer class in c#. I want to do following: I write my logs in the 1st Logfile, if it is over a certain size, it will write in the 2nd logfile. If the 2nd logfile is full, then delete the 1st one and write in that one again and so on.

I've got this so far:

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

can anyone help me complete it please?

Was it helpful?

Solution

It took me some time but (finally) after a few changes your code is now working as required.

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

btw, you should look into log4net (as also suggested by martin_costello), it has a lot of features and lots of flexibility. Refer: Apache log4net features list

OTHER TIPS

I think it would be something like this:

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);    
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top