title: How to write text to txt file in web service using c#?
body: <p>I have a web service.This service insert data to sql from some clients.I tried write inserted row and any Exception to a txt file.</p>
  
  <pre><code>void WriteLog(string Log)
  {
      string FileName= "D:\\MyLogs.txt";
      StreamWriter obj;
      obj= new StreamWriter(FileName);
      obj.WriteLine(DateTime.Now.ToString() + " " + Log);
      obj.Close();
  }
  
  int row;
  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString);
  
  SqlCommand cmd = new SqlCommand();
  
  [WebMethod]
  public int InsertTable(int Age) {
      try {
          con.Open();
          cmd.CommandText=@"Insert into Table(Age) values(@Age)";
          cmd.Parameters.Add(new SqlParameter("Age", SqlDbType.Int, 32, "Age"));
          cmd.Parameters["Age"].Value = Age;
          cmd.Connection = con;
          cmd.ExecuteNonQuery();
          row = cmd.ExecuteNonQuery();
  
          WriteLog(row.ToString());    
      }
      catch (Exception ex) {
          WriteLog(ex.ToString());    
      }
      finally {
          con.Close();
          con.Dispose();
          cmd.Dispose();
      }
      return row;
  }
  </code></pre>
  
  <p>I get this error : 'D:\MyLogs.txt' Access to the path is denied.</p>