title: Error when writing to log while threading on web requests
body: <p>I am building a program in visual studio 2010 using C# .Net4.0</p>
  
  <p>I have a function that takes in a url and try to get a response. Inside the function, I write to a log file stating successes and errors.</p>
  
  <pre><code>public void getResponse(string url, System.IO.StreamWriter writer)
  {
      try
      {
          WebRequest request = WebRequest.Create(url);
          WebResponse response = request.GetResponse();
  
          // Display the status.
          Console.WriteLine(response.Headers);
          writeToLog("Success: " + url, writer);
      }
      catch (WebException ex)
      {
          writeToLog("Error: " + ex.message, writer);
      }
  }
  </code></pre>
  
  <p>I have a list of urls, and I use Parallel.ForEach to run multiple urls at once.</p>
  
  <pre><code>System.IO.StreamWriter writer = new System.IO.StreamWriter("program.log", true);
  string[] urls = {url1, url2, url3, url4, etc}; //have around 30 urls
  Parallel.ForEach(urls, url =&gt; getResponse(url, writer));
  </code></pre>
  
  <p>When I run the program, sometime I will get an error saying that program.log is being used by another program. This doesn't happen all the time, but once a while it will happen and the program will crash.</p>
  
  <p><strong>Question:</strong> What should I do to avoid this error? Or, is there an alternative way of doing it?</p>