首页 > 其他 > 详细

收集错误日志方法

时间:2019-05-25 00:47:33      阅读:142      评论:0      收藏:0      [点我收藏+]
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());

收集错误日志方法

原文:https://www.cnblogs.com/qiu18359243869/p/10920857.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!