A litle log util

A lots of time you will need to log your exceptions or debugging info to a file. Then this litle log util comes in handy:

// simple log writer
    public class LogWriter
    {
        // privates
        private static LogWriter logWriter = null;
        private string exePath = string.Empty;
        private string fileName = "DictionaryMergerLog.txt";
        private string fullFileName = string.Empty;
        private static bool wait = false;

        // ctors
        private LogWriter()
        {
            CheckFileExistenceAndSize();
        }

        public static LogWriter Instance
        {
            get
            {
                if (logWriter == null)
                {
                    logWriter = new LogWriter();
                }
                return logWriter;
            }
        }

        // methods
        public void Log(string logMessage)
        {
            try
            {
                CheckFileExistenceAndSize();

                using (var w = File.AppendText(fullFileName))
                {
                    Log(logMessage, w);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error writing log message" + ex.Message);
            }
        }

        private void Log(string logMessage, TextWriter txtWriter)
        {
            txtWriter.WriteLine("{0} :{1}", DateTime.Now.ToString("dd HH:mm:ss"), logMessage);
        }

        private void CheckFileExistenceAndSize()
        {
            // detect and or create log dir
            exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            exePath = Path.Combine(@exePath, "Log");
            if (!Directory.Exists(exePath))
            {
                Directory.CreateDirectory(exePath);
            }

            // detect and or create logfile
            fullFileName = Path.Combine(exePath, fileName);

            // detect size of file
            if (File.Exists(fullFileName))
            {
                if (File.ReadAllBytes(fullFileName).Length >= 10 * 1024 * 1024) // (10mB) File to big? Create new
                {
                    // if there no other log-files
                    if (new DirectoryInfo(exePath).GetFiles().Count() == 1)
                    {
                        CreateArchiveLog("1");
                    }
                    else
                    {
                        var highestLogFile =
                            new DirectoryInfo(exePath).GetFiles().Where(a => a.Name != "DictionaryMergerLog.txt").OrderByDescending(a => a.Name).First().Name;
                        var foundLogNumber = highestLogFile.Split('-')[0];

                        if (string.IsNullOrEmpty(foundLogNumber))
                        {
                            throw new ArgumentNullException("Cannot create a new log-archive for an empty log-number");
                        }
                        // increade the found log number
                        var increasedFoundLogNumber = (int.Parse(foundLogNumber) + 1).ToString();
                        CreateArchiveLog(increasedFoundLogNumber);
                    }
                }
            }
            //No File? Create
            if (!File.Exists(fullFileName))
            {
                File.Create(fullFileName).Close();
            }
        }

        private void CreateArchiveLog(string archiveLogNumber)
        {
            // rename current file
            var newFileName = archiveLogNumber + "-" + fileName;
            var newFullFileName = Path.Combine(exePath, newFileName);
            File.Move(fullFileName, newFullFileName);
        }
    } 

Recent

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!