Wednesday 1 October 2014

Create a new log file When the Size of Current log file Crosses size limit

Logging messages is always a good practice in development.You can check the log file to know where the application is breaking.Sometimes a log file becomes huge in size and takes some time to open.

Below is C# code to create a new log file When the Size of Current log file Cross size limit.You can define your maximum size of file say 5 MB.
       

          public static void writeLog(string text)
        {
            try
            {
                long length = 0;
                string fileName = "My_Logger_1" + "_" + DateTime.Now.Year.ToString("0000") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Day.ToString("00") + ".log";
                string filePath = string.Empty;
                string[] logfileVersion = new string[] { };

                string date = System.DateTime.Now.Year.ToString() + "_" + System.DateTime.Now.Month.ToString() + "_" + System.DateTime.Now.Day.ToString();
                string logpath = @"D:\Logger";
                int MAX_FILE_SIZE = 5242880;//5MB

                filePath = logpath + "\\" + fileName;



                if (File.Exists(filePath))
                {
                     List<String> files = getTodaysFiles(logpath);

                    length = new System.IO.FileInfo(files[files.Count - 1]).Length;
                    logfileVersion = Path.GetFileName(files[files.Count - 1]).Split('_');
                    filePath = files[files.Count - 1];
                }


                StreamWriter LogWriter = new StreamWriter(new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read));

                if (length > MAX_FILE_SIZE)
                {
                   
                    int nextVersion = Convert.ToInt32(logfileVersion[2]) + 1;
                    LogWriter.Close();

                    LogWriter = new System.IO.StreamWriter(new FileStream(string.Format(
                    logpath + "\\My_Logger_{0}" + "_" + DateTime.Now.Year.ToString("0000") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Day.ToString("00") + ".log", nextVersion), FileMode.Append, FileAccess.Write, FileShare.Read));

                }
                LogWriter.WriteLine(DateTime.Now.ToString() + "  -  "+text);
                LogWriter.Close();


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<String> getTodaysFiles(String folderPath)
        {
            List<String> todaysFiles = new List<String>();
            foreach (String file in Directory.GetFiles(folderPath))
            {
                DirectoryInfo di = new DirectoryInfo(file);
                if (di.CreationTime.ToShortDateString().Equals(DateTime.Now.ToShortDateString()) && di.Name.Contains("My_Logger"))
                {
                    todaysFiles.Add(file);

                }

            }
            return todaysFiles;
        }