Click here to Skip to main content
15,891,597 members
Home / Discussions / C#
   

C#

 
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 
FileShare.Read should be sufficient to prevent multiple processes from writing to the file at the same time, and that's the mode that File.AppendAllLines uses.

Within a single AppDomain, your class should be perfectly thread-safe - everything is wrapped in a monitor on the same guard object, so only one thread can be executing the code at a time.

With different AppDomains, or different processes, things get more complicated. Your code can be executing on threads from different AppDomains or processes at the same time, because they have their own copies of the buffer and the guard object. The problem you will encounter is that the FileStream constructor (or the AppendAllLines method) will throw an exception if another process is writing to the file when you try to open it.

There's no easy way around that last problem, which is why most logging frameworks don't tend to write to the same file from different processes.

If you need multiple processes logging to the same place at the same time, you might want to consider the Event Log, and possibly look at using the Microsoft.Diagnostics.Tracing.EventSource library[^] (NuGet[^] | MSDN[^]) to simplify your code.



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

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.