I am using a System.Threading.ThreadPool to manage a queue of jobs from a service. I have already implemented logging like this...
abstract class Global
{
public static LogFile LogFile = null;
}
public class LogFile : IDisposable
{
private StreamWriter sw;
public LogFile(string path){}
public void WriteEntry(string logText)
{
lock (sw)
{
sw.WriteLine(logText);
}
}
}
I want to create the log at service startup and use it from my queued worker threads.. something like this...
//On Service Start
Global.LogFile = new LogFile("log.txt");
//Kick of worker thread
ThreadPool.QueueUserWorkItem(objWrkrThread.CallbackMethod, iCount);
//Worker thread logs an entry in CallbackMethod()
Global.LogFile.WriteEntry("Hello World");
Is this safe? Will calling a method on a static instance of a class inadvertently 'synchronise' or 'block' my threads?
Michael