欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

写日志

程序员文章站 2022-06-07 10:59:52
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();
}