Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
AnswerRe: KeyDown for Form and all its Controls Pin
BillWoodruff21-Oct-15 15:02
professionalBillWoodruff21-Oct-15 15:02 
AnswerRe: KeyDown for Form and all its Controls Pin
Dave Kreskowiak21-Oct-15 17:15
mveDave Kreskowiak21-Oct-15 17:15 
AnswerRe: KeyDown for Form and all its Controls Pin
John Torjo21-Oct-15 23:18
professionalJohn Torjo21-Oct-15 23:18 
SuggestionRe: KeyDown for Form and all its Controls Pin
Foothill23-Oct-15 10:20
professionalFoothill23-Oct-15 10:20 
Questionc# Pin
Member 1061105521-Oct-15 8:11
Member 1061105521-Oct-15 8:11 
GeneralRe: c# Pin
harold aptroot21-Oct-15 8:42
harold aptroot21-Oct-15 8:42 
AnswerRe: c# Pin
cvogt6145721-Oct-15 8:48
cvogt6145721-Oct-15 8:48 
Questionthread safe log class with simple functionality Pin
Member 1206160021-Oct-15 5:57
Member 1206160021-Oct-15 5:57 
Can I ask you your opinion if you see some major problems with this class I wrote? As you can see this is meant to be thread safe log class.
I don't want to use 3rd party solution. Thanks.

C#
   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();
            }



    }
}

AnswerRe: thread safe log class with simple functionality Pin
BillWoodruff21-Oct-15 7:08
professionalBillWoodruff21-Oct-15 7:08 
GeneralRe: thread safe log class with simple functionality Pin
Member 1206160021-Oct-15 7:36
Member 1206160021-Oct-15 7:36 
GeneralRe: thread safe log class with simple functionality Pin
BillWoodruff21-Oct-15 7:45
professionalBillWoodruff21-Oct-15 7:45 
GeneralRe: thread safe log class with simple functionality Pin
Member 1206160021-Oct-15 7:51
Member 1206160021-Oct-15 7:51 
GeneralRe: thread safe log class with simple functionality Pin
BillWoodruff21-Oct-15 7:58
professionalBillWoodruff21-Oct-15 7:58 
GeneralRe: thread safe log class with simple functionality Pin
Member 1206160021-Oct-15 8:29
Member 1206160021-Oct-15 8:29 
AnswerRe: thread safe log class with simple functionality Pin
Richard Deeming21-Oct-15 8:09
mveRichard Deeming21-Oct-15 8:09 
GeneralRe: thread safe log class with simple functionality Pin
Member 1206160021-Oct-15 8:16
Member 1206160021-Oct-15 8:16 
GeneralRe: thread safe log class with simple functionality Pin
Richard Deeming21-Oct-15 9:06
mveRichard Deeming21-Oct-15 9:06 
GeneralRe: thread safe log class with simple functionality Pin
Member 1206160021-Oct-15 9:33
Member 1206160021-Oct-15 9:33 
GeneralRe: thread safe log class with simple functionality Pin
Richard Deeming22-Oct-15 2:01
mveRichard Deeming22-Oct-15 2:01 
GeneralRe: thread safe log class with simple functionality Pin
Member 1206160022-Oct-15 5:24
Member 1206160022-Oct-15 5:24 
GeneralRe: thread safe log class with simple functionality Pin
Richard Deeming22-Oct-15 5:35
mveRichard Deeming22-Oct-15 5:35 
GeneralRe: thread safe log class with simple functionality Pin
Member 1206160022-Oct-15 6:31
Member 1206160022-Oct-15 6:31 
GeneralRe: thread safe log class with simple functionality Pin
Richard Deeming22-Oct-15 6:52
mveRichard Deeming22-Oct-15 6:52 
GeneralRe: thread safe log class with simple functionality Pin
Member 1206160022-Oct-15 7:07
Member 1206160022-Oct-15 7:07 
GeneralRe: thread safe log class with simple functionality Pin
Richard Deeming22-Oct-15 7:18
mveRichard Deeming22-Oct-15 7:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.