我试图创建一个线程安全的方法。
有人看到以下代码有任何并发问题吗?
对我来说这似乎很好,尽管我永远无法找到一种方法来测试这种方法的并发性。任何帮助,将不胜感激。
//static locker for thread synchronization
private static readonly System.Object _object3 = new System.Object();
//this method needs to be thread-safe
public static void LogToTextFile(string logMessage, LogLevel logType)
{
//make sure only one thread executes this file writing code at a time
lock (_object3)
{
using (StreamWriter w = File.AppendText(@"c:\logs\log1.txt");
{
w.WriteLine("\r\n{0} logged at {1} {2} : {3}", logType.ToString().ToUpper(), DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString(), logMessage);
}
}
}