这个自定义日志记录线程安全?

时间:2016-06-02 07:16:47

标签: c#

我有一个自定义日志记录例程,并想检查它是否是线程安全的,因为我将从几个事件中调用它。入口点将是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);
   }
  }


 }

0 个答案:

没有答案