Thursday, September 27, 2012

Creating and renaming Logs when the size exceeds 400 MB


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace readtxt
{
    public class CreateLog
    {
        string fileName = "";
        public string CreateLogFile()
        {
            return getsizeofFile();

        }

        public string getsizeofFile()
        {
            fileName = @"C:\Program Files\Virtual Payment Solutions\TSS 1.5\Retail.log";
            FileInfo f = new FileInfo(fileName);
            long s1 = f.Length;
            string size = GetFileSize(s1);
            createNewLogFile(size, fileName);
            return size;
        }

        public void createNewLogFile(string size, string fileLoc)
        {
            FileStream fs = null;
            if (size.Contains("MB"))
            {
                Regex re = new Regex(@"\d+");
                Match m = re.Match(size);
                if (Convert.ToInt64(m.Value) >= 400)
                {
                    string fileLocCopy = @"C:\Program Files\Virtual Payment Solutions\TSS 1.5\Retail_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log";
                    if (File.Exists(fileLoc))
                    {
                        File.Move(fileLoc, fileLocCopy);
                        if (File.Exists(fileLoc))
                        {
                            File.Delete(fileLoc);
                            using (fs = File.Create(fileLoc))
                            {

                            }
                        }
                        else if (!File.Exists(fileLoc))
                        {
                            using (fs = File.Create(fileLoc))
                            {

                            }
                        }
                    }
                    else if (!File.Exists(fileLoc))
                    {
                        using (fs = File.Create(fileLoc))
                        {

                        }
                    }
                }

            }

        }

        public static string GetFileSize(long Bytes)
        {
            if (Bytes >= 1073741824)
            {
                Decimal size = Decimal.Divide(Bytes, 1073741824);
                return String.Format("{0:##.##} GB", size);
            }
            else if (Bytes >= 1048576)
            {
                Decimal size = Decimal.Divide(Bytes, 1048576);
                return String.Format("{0:##.##} MB", size);
            }
            else if (Bytes >= 1024)
            {
                Decimal size = Decimal.Divide(Bytes, 1024);
                return String.Format("{0:##.##} KB", size);
            }
            else if (Bytes > 0 & Bytes < 1024)
            {
                Decimal size = Bytes;
                return String.Format("{0:##.##} Bytes", size);
            }
            else
            {
                return "0 Bytes";
            }
        }
    }

}

No comments:

Post a Comment