Click here to Skip to main content
15,902,189 members
Home / Discussions / C#
   

C#

 
Questionthread safe log class with simple functionality Pin
Member 1206160021-Oct-15 5:57
Member 1206160021-Oct-15 5:57 
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 
The first thing that jumps out at me:
C#
try
{
    ...
}
catch (Exception ex)
{
    throw;
}

You've avoided the common mistake of using throw ex;, which would destroy the call-stack; but you're not doing anything with the exception that you've caught. You should remove the entire try..catch and replace it with the contents of the try block.

You're calling buffer.Count(), which is an extension method provided by LINQ. Although it has special-case code for collections which implement ICollection<T>, it would be cleaner to just use the Count property directly on the list.

Use string.Format to build the file name would make the code easier to read:
C#
var todaysLogFilePath = Path.Combine(_logDir, string.Format("Log{0:yyyy-MMMM-dd}.txt", DateTime.Today));

Alternatively, if you're using .NET 4.6, an interpolated string would be even cleaner:
C#
var todaysLogFilePath = Path.Combine(_logDir, $"Log{DateTime.Today:yyyy-MMMM-dd}.txt");


I'd be inclined to replace the whole FileStream / StreamWriter block with a single call to File.AppendAllLines:
C#
var todaysLogFilePath = ...;
File.AppendAllLines(todaysLogFilePath, buffer);
buffer.Clear();




"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 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 
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 
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 

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.