当前位置: 移动技术网 > IT编程>开发语言>c# > 写日志

写日志

2019年10月16日  | 移动技术网IT编程  | 我要评论
public void WriteLog(string strLog) { string sFilePath = AppDomain.CurrentDomain.BaseDirectory + "Log"; string sFileName = DateTime.Now.ToString("yyyy ...

public void writelog(string strlog)
{
  string sfilepath = appdomain.currentdomain.basedirectory + "log";
  string sfilename = datetime.now.tostring("yyyymmdd") + ".txt";
  sfilename = sfilepath + "\\" + sfilename; //文件的绝对路径
  if (!directory.exists(sfilepath))//验证路径是否存在
  {
    directory.createdirectory(sfilepath);
    //不存在则创建
  }
  filestream fs;
  streamwriter sw;
  if (file.exists(sfilename))
  //验证文件是否存在,有则追加,无则创建
  {
    fs = new filestream(sfilename, filemode.append, fileaccess.write);
  }
  else
  {
    fs = new filestream(sfilename, filemode.create, fileaccess.write);
  }
  sw = new streamwriter(fs);
  sw.writeline(datetime.now.tostring("yyyy-mm-dd hh:mm:ss") + " --- " + strlog);
  sw.close();
  fs.close();
}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网