Hello i have problem with putting logs using multithreading, my function loks like this
public static void AddToLog(string message)
{
using (FileStream fs = new FileStream(ExeDir + @"\logs.txt", FileMode.OpenOrCreate, FileAccess.Write))
{
using (StreamWriter mStreamWriter = new StreamWriter(fs))
{
mStreamWriter.BaseStream.Seek(0, SeekOrigin.End);
mStreamWriter.WriteLine(message + " at " + DateTime.Now.ToLongTimeString() + "." +
DateTime.Now.Millisecond);
mStreamWriter.Flush();
}
}
}
And well , i create this object each time, so i can’t lock it, problem is when i am opening it, what is the best way to do it?
Your method is already static, if you are using one instance of the application just add
But I think @Mark has the better advice to use a special library.