title: Append text in exclusively opened file
body: <p>I am trying to implement locking scenario where same file is accessed by multiple threads at same time or at different time. While testing it seems code is overwriting the existing line instead of appending new line. </p>
  
  <pre><code>namespace SMPPService
  {
  public static class LogFile
  {
      public static void WriteErrorLog(Exception ex)
      {
          byte[] buf = GetBytes(DateTime.Now.ToString() + ": " + ex.Source.ToString().Trim() + "; " + ex.Message.ToString().Trim());
  
          Lock(HttpRuntime.AppDomainAppPath + "\\Exceptions.txt",
                  (f) =&gt;
                  {
                      try
                      {
                          f.Write(buf, 0, buf.Length);
                      }
                      catch (IOException ioe)
                      {
                          // handle IOException
                      }
                  }, buf);
      }
  
      public static void WriteErrorLog(string Message)
      {
          byte[] buf = GetBytes(DateTime.Now.ToString() + ": " + Message);           
  
          Lock(HttpRuntime.AppDomainAppPath + "\\LogFile.txt",
                  (f) =&gt;
                  {
                      try
                      {
                          f.Write(buf, 0, buf.Length);
                      }
                      catch (IOException ioe)
                      {
                          // handle IOException
                      }
                  }, buf);
  
          System.Threading.Thread.Sleep(60000);
  
      }
  
      public static void Lock(string path, Action&lt;FileStream&gt; action,byte [] lines)
      {
          var autoResetEvent = new AutoResetEvent(false);
  
          while (true)
          {
              try
              {
                  using (var file = File.Open(path,
                                              FileMode.OpenOrCreate,
                                              FileAccess.ReadWrite,
                                              FileShare.Write))
                  {
                      action(file);                              
                      break;
                  }
              }
              catch (IOException)
              {
                  var fileSystemWatcher =
                      new FileSystemWatcher(Path.GetDirectoryName(path))
                      {
                          EnableRaisingEvents = true
                      };
  
                  fileSystemWatcher.Changed +=
                      (o, e) =&gt;
                      {
                          if (Path.GetFullPath(e.FullPath) == Path.GetFullPath(path))
                          {
                              autoResetEvent.Set();
                          }
                      };
  
                  autoResetEvent.WaitOne();
              }
          }
      }
      static byte[] GetBytes(string str)
      {
          byte[] bytes = new byte[str.Length * sizeof(char)];
          System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
          return bytes;
      }
   }
   }
  </code></pre>
  
  <p><strong>Usage</strong></p>
  
  <pre><code>LogFile.WriteErrorLog("Requesting SMPP Client from WMAS..." + " " + "Date:" + DateTime.Now + " " + "Source Address:" + msisdn);
  </code></pre>