Skip to main content
Tweeted twitter.com/StackCodeReview/status/1392404189054226434
added 148 characters in body
Source Link
user87512
user87512

And in general I am interested if its safe to use my class because some people always say to not create your own logging class. Is it ok if I use this class?And in general I am interested if its safe to use my class because some people always say to not create your own logging class. Is it ok if I use this class? I see people creating log classes such as here http://www.codeproject.com/Tips/585796/Simple-Log, so what is problem if I created one as above?

And in general I am interested if its safe to use my class because some people always say to not create your own logging class. Is it ok if I use this class?

And in general I am interested if its safe to use my class because some people always say to not create your own logging class. Is it ok if I use this class? I see people creating log classes such as here http://www.codeproject.com/Tips/585796/Simple-Log, so what is problem if I created one as above?

Rollback to Revision 4
Source Link
Mathieu Guindon
  • 75.1k
  • 18
  • 191
  • 463
   static class GMLogger
    {
        
       private static List<string> _buffer = new List<string>();
       private static int _maxBufferSize = 25;  // number of messages  permitted in the buffer at a time
       public static string _logDir = "C:\\Logs";
       private static readonly object _syncObject = new object();
         
       // Log message
        public static  void Log(string logMessage)
        {
            try
            {
                lock (_syncObject)
                {
                    _buffer.Add(logMessage);

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

       // Save buffer if needed
        private static void Save(List<string> buffer)
        {
            if (buffer.Count > _maxBufferSize)
            {
                // Write to file
                if (!_logDir.EndsWith("\\")) _logDir += "\\"; 
                DirectoryInfo di = Directory.CreateDirectory(_logDir);
                var todaysLogFilePath = Path.Combine(_logDir,
                                                       ("Log" + DateTime.Now.ToString("yyyy-MMMM-dd") + ".txt"));
                using (FileStream aFile = new FileStream(todaysLogFilePath, FileMode.Append, FileAccess.Write, FileShare.None))
                using (StreamWriter sw = new StreamWriter(aFile))
                {
                    for (int i = 0; i < buffer.Count(); i++)
                    {
                        sw.WriteLine(buffer[i]);
                    }
                }

                // Clear buffer
                buffer.Clear();
            }
        

        
    }
}
 class GMLogger
{

    private static List<string> _buffer = new List<string>();
    private static int _maxBufferSize = 25;  // number of messages  permitted in the buffer at a time
    public  string _logDir = "C:\\Logs";
    private static readonly object _syncObject = new object();

    // Log message
    public void Log(string logMessage)
    {
        try
        {
            lock (_syncObject)
            {
                _buffer.Add(logMessage);

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

    // Save buffer if needed
    private void Save(List<string> buffer)
    {
        if (buffer.Count > _maxBufferSize)
        {
            // Write to file
            if (!_logDir.EndsWith("\\")) _logDir += "\\";
            DirectoryInfo di = Directory.CreateDirectory(_logDir);
            var todaysLogFilePath = Path.Combine(_logDir,
                                                   ("Log" + DateTime.Now.ToString("yyyy-MMMM-dd") + ".txt"));
            using (FileStream aFile = new FileStream(todaysLogFilePath, FileMode.Append, FileAccess.Write, FileShare.None))
            using (StreamWriter sw = new StreamWriter(aFile))
            {
                for (int i = 0; i < buffer.Count(); i++)
                {
                    sw.WriteLine(buffer[i]);
                }
            }

            // Clear buffer
            buffer.Clear();
        }



    }
}
   static class GMLogger
    {
        
       private static List<string> _buffer = new List<string>();
       private static int _maxBufferSize = 25;  // number of messages  permitted in the buffer at a time
       public static string _logDir = "C:\\Logs";
       private static readonly object _syncObject = new object();
         
       // Log message
        public static  void Log(string logMessage)
        {
            try
            {
                lock (_syncObject)
                {
                    _buffer.Add(logMessage);

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

       // Save buffer if needed
        private static void Save(List<string> buffer)
        {
            if (buffer.Count > _maxBufferSize)
            {
                // Write to file
                if (!_logDir.EndsWith("\\")) _logDir += "\\"; 
                DirectoryInfo di = Directory.CreateDirectory(_logDir);
                var todaysLogFilePath = Path.Combine(_logDir,
                                                       ("Log" + DateTime.Now.ToString("yyyy-MMMM-dd") + ".txt"));
                using (FileStream aFile = new FileStream(todaysLogFilePath, FileMode.Append, FileAccess.Write, FileShare.None))
                using (StreamWriter sw = new StreamWriter(aFile))
                {
                    for (int i = 0; i < buffer.Count(); i++)
                    {
                        sw.WriteLine(buffer[i]);
                    }
                }

                // Clear buffer
                buffer.Clear();
            }
        

        
    }
}
deleted 242 characters in body
Source Link
user87512
user87512
   static class GMLogger
    {
        
       private static List<string> _buffer = new List<string>();
       private static int _maxBufferSize = 25;  // number of messages  permitted in the buffer at a time
       public static string _logDir = "C:\\Logs";
       private static readonly object _syncObject = new object();
         
       // Log message
        public static  void Log(string logMessage)
        {
            try
            {
                lock (_syncObject)
                {
                    _buffer.Add(logMessage);

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

       // Save buffer if needed
        private static void Save(List<string> buffer)
        {
            if (buffer.Count > _maxBufferSize)
            {
                // Write to file
                if (!_logDir.EndsWith("\\")) _logDir += "\\"; 
                DirectoryInfo di = Directory.CreateDirectory(_logDir);
                var todaysLogFilePath = Path.Combine(_logDir,
                                                       ("Log" + DateTime.Now.ToString("yyyy-MMMM-dd") + ".txt"));
                using (FileStream aFile = new FileStream(todaysLogFilePath, FileMode.Append, FileAccess.Write, FileShare.None))
                using (StreamWriter sw = new StreamWriter(aFile))
                {
                    for (int i = 0; i < buffer.Count(); i++)
                    {
                        sw.WriteLine(buffer[i]);
                    }
                }

                // Clear buffer
                buffer.Clear();
            }
        

        
    }
}
   static class GMLogger
    {
        
       private static List<string> _buffer = new List<string>();
       private static int _maxBufferSize = 25;  // number of messages  permitted in the buffer at a time
       public static string _logDir = "C:\\Logs";
       private static readonly object _syncObject = new object();
         
       // Log message
        public static  void Log(string logMessage)
        {
            try
            {
                lock (_syncObject)
                {
                    _buffer.Add(logMessage);

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

       // Save buffer if needed
        private static void Save(List<string> buffer)
        {
            if (buffer.Count > _maxBufferSize)
            {
                // Write to file
                if (!_logDir.EndsWith("\\")) _logDir += "\\"; 
                DirectoryInfo di = Directory.CreateDirectory(_logDir);
                var todaysLogFilePath = Path.Combine(_logDir,
                                                       ("Log" + DateTime.Now.ToString("yyyy-MMMM-dd") + ".txt"));
                using (FileStream aFile = new FileStream(todaysLogFilePath, FileMode.Append, FileAccess.Write, FileShare.None))
                using (StreamWriter sw = new StreamWriter(aFile))
                {
                    for (int i = 0; i < buffer.Count(); i++)
                    {
                        sw.WriteLine(buffer[i]);
                    }
                }

                // Clear buffer
                buffer.Clear();
            }
        

        
    }
}
 class GMLogger
{

    private static List<string> _buffer = new List<string>();
    private static int _maxBufferSize = 25;  // number of messages  permitted in the buffer at a time
    public  string _logDir = "C:\\Logs";
    private static readonly object _syncObject = new object();

    // Log message
    public void Log(string logMessage)
    {
        try
        {
            lock (_syncObject)
            {
                _buffer.Add(logMessage);

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

    // Save buffer if needed
    private void Save(List<string> buffer)
    {
        if (buffer.Count > _maxBufferSize)
        {
            // Write to file
            if (!_logDir.EndsWith("\\")) _logDir += "\\";
            DirectoryInfo di = Directory.CreateDirectory(_logDir);
            var todaysLogFilePath = Path.Combine(_logDir,
                                                   ("Log" + DateTime.Now.ToString("yyyy-MMMM-dd") + ".txt"));
            using (FileStream aFile = new FileStream(todaysLogFilePath, FileMode.Append, FileAccess.Write, FileShare.None))
            using (StreamWriter sw = new StreamWriter(aFile))
            {
                for (int i = 0; i < buffer.Count(); i++)
                {
                    sw.WriteLine(buffer[i]);
                }
            }

            // Clear buffer
            buffer.Clear();
        }



    }
}
added 161 characters in body
Source Link
user87512
user87512
Loading
edited title
Link
user87512
user87512
Loading
edited tags
Link
SuperBiasedMan
  • 13.1k
  • 5
  • 36
  • 62
Loading
Source Link
user87512
user87512
Loading