public static void writelog(string log)
{
  streamwriter fw = null;
  //string filepath = directory.getcurrentdirectory();
  string filepath = appdomain.currentdomain.basedirectory;
  if (!filepath.endswith(@"\resource"))
  {
      filepath = filepath + "resource";
  }
  string filename = filepath + "\\log.txt";
  try
  {
      if (file.exists(filename))
      {
          fw = file.appendtext(filename);
      }
      else
      {
          fw = file.createtext(filename);
      }
      fw.writeline(log);
  }
  catch (exception e) { }
  finally
  {
      if (fw != null)
      {
          try { fw.close(); } catch (exception e1) { }
      }
  }
}
//resource 文件夹下有个log.txt文件,用于记录错误日志

在其他方法中调用:

writelog(“时间:” + datetime.now.tostring());
writelog(“xxx 方法错误”);
writelog(“error:” + e.tostring().trim());