我有一个自定义日志记录例程,并想检查它是否是线程安全的,因为我将从几个事件中调用它。入口点将是logevents(消息)
using System.Threading;
using System.IO;
public class mystuff {
private static object writeLock = new object();
private static string eventsLogfile = "";
private void logEvent(object message) {
// To enqueue the write
ThreadPool.QueueUserWorkItem(WriteToFile, DateTime.Now + " " + Instrument.FullName + " " + message.ToString());
}
private void WriteToFile(object msg) {
lock(writeLock) {
using(var writer = File.AppendText(eventsLogfile)) {
writer.WriteLine((string) msg);
}
}
}