收集错误日志方法

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());
posted @ 2019-05-25 00:00  厦门哈韩  阅读(280)  评论(0编辑  收藏  举报