Click here to Skip to main content
15,911,035 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: thread safe log class with simple functionality Pin
Member 1206160022-Oct-15 10:36
Member 1206160022-Oct-15 10:36 
GeneralRe: thread safe log class with simple functionality Pin
Member 1206160022-Oct-15 6:35
Member 1206160022-Oct-15 6:35 
GeneralRe: thread safe log class with simple functionality Pin
Richard Deeming22-Oct-15 6:54
mveRichard Deeming22-Oct-15 6:54 
GeneralRe: thread safe log class with simple functionality Pin
Richard Deeming22-Oct-15 2:39
mveRichard Deeming22-Oct-15 2:39 
There are some good points on that StackExchange thread - particularly points 1 and 3 in Nikita Brizhak's answer, which address some incorrect behaviour in the class.

Fixing point 1 is easy - copy the _logDir field to a variable at the start of the method, and only use that variable:
C#
string logDir = _logDir;
if (!logDir.EndsWith(@"\")) logDir += @"\"; 
Directory.CreateDirectory(logDir);
var todaysLogFilePath = Path.Combine(logDir, ...);


Fixing point 3 is slightly harder. You need to make sure that any entries in the buffer are saved when the application exits, regardless of the number of entries. To do that, you'll need to split the code that saves the entries from the code that tests the number of entries, as EBrown suggested.
C#
static class GMLogger
{
    private const int MaxBufferSize = 25;
    private static readonly object _syncObject = new object();
    private static readonly List<string> _buffer = new List<string>();
    private static string _logDir = @"C:\Logs\";
    
    public static string LogDirectory
    {
        get
        {
            return _logDir;
        }
        set
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentNullException("value");
            }
            if (!value.EndsWith(@"\"))
            {
                value += @"\";
            }
            
            _logDir = value;
        }
    }
    
    private static string EnsureLogDirectory()
    {
        string path = LogDirectory;
        Directory.CreateDirectory(path);
        return path;
    }
    
    private static string BuildLogPath(string directory, DateTime logDate)
    {
        string fileName = string.Format("Log{0:yyyy-MM-dd}.txt", logDate);
        return Path.Combine(directory, fileName);
    }
    
    private static void SaveLog()
    {
        if (_buffer.Count != 0)
        {
            string directory = EnsureLogDirectory();
            string fileName = BuildLogPath(directory, DateTime.Today);
            File.AppendAllLines(fileName, _buffer);
            _buffer.Clear();
        }
    }
    
    public static void Log(string logMessage)
    {
        lock (_syncObject)
        {
            _buffer.Add(logMessage);
            
            if (_buffer.Count == MaxBufferSize)
            {
                SaveLog();
            }
        }
    }
    
    public static void Save()
    {
        lock (_syncObject)
        {
            SaveLog();
        }
    }
}

You'd then need to make sure your Main method calls GMLogger.Save() before it returns. A finally block would probably be the best place for that:
C#
static void Main()
{
    try
    {
        ...
    }
    finally
    {
        GMLogger.Save();
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: thread safe log class with simple functionality Pin
Member 1206160022-Oct-15 5:34
Member 1206160022-Oct-15 5:34 
GeneralRe: thread safe log class with simple functionality Pin
Richard Deeming22-Oct-15 5:37
mveRichard Deeming22-Oct-15 5:37 
AnswerRe: thread safe log class with simple functionality Pin
John Torjo21-Oct-15 23:08
professionalJohn Torjo21-Oct-15 23:08 
QuestionTabelLayoutPanel Resizing at Runtime c#.net Pin
Darshan BS20-Oct-15 21:47
Darshan BS20-Oct-15 21:47 
AnswerRe: TabelLayoutPanel Resizing at Runtime c#.net Pin
OriginalGriff20-Oct-15 22:31
mveOriginalGriff20-Oct-15 22:31 
GeneralRe: TabelLayoutPanel Resizing at Runtime c#.net Pin
Darshan BS20-Oct-15 22:41
Darshan BS20-Oct-15 22:41 
GeneralRe: TabelLayoutPanel Resizing at Runtime c#.net Pin
OriginalGriff20-Oct-15 22:50
mveOriginalGriff20-Oct-15 22:50 
AnswerRe: TabelLayoutPanel Resizing at Runtime c#.net Pin
BillWoodruff21-Oct-15 2:01
professionalBillWoodruff21-Oct-15 2:01 
Questionneed help. No error in code but not getting output. here is my code. trying to accessing data from active directory. Pin
rahul38420-Oct-15 17:31
rahul38420-Oct-15 17:31 
AnswerRe: need help. No error in code but not getting output. here is my code. trying to accessing data from active directory. Pin
Richard MacCutchan20-Oct-15 21:38
mveRichard MacCutchan20-Oct-15 21:38 
AnswerRe: need help. No error in code but not getting output. here is my code. trying to accessing data from active directory. Pin
Richard Deeming21-Oct-15 1:48
mveRichard Deeming21-Oct-15 1:48 
QuestionWindows Remote Application is not working Pin
Josephss7318-Oct-15 22:35
Josephss7318-Oct-15 22:35 
QuestionHow should I access share folder from server Pin
Member 1201610618-Oct-15 21:10
Member 1201610618-Oct-15 21:10 
AnswerRe: How should I access share folder from server Pin
OriginalGriff18-Oct-15 23:12
mveOriginalGriff18-Oct-15 23:12 
Question[WIN2D] Randomly generated 2D drawing Pin
Member 858597418-Oct-15 14:10
Member 858597418-Oct-15 14:10 
Questionc# delegates and events Pin
Lillepige17-Oct-15 6:06
Lillepige17-Oct-15 6:06 
AnswerRe: c# delegates and events Pin
BillWoodruff17-Oct-15 6:25
professionalBillWoodruff17-Oct-15 6:25 

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.