using System.IO;
//捕获异常写入Log catch (Exception ex) { string msg = ex.Message + ex.StackTrace; string path = @"E:log.txt"; write(path, msg); } public void Write(string str_path,string str_msg) { //FileMode.Append为不覆盖文件效果.create为覆盖 FileStream fs = new FileStream(str_path, FileMode.Append); string str_time = DateTime.Now.ToString(); //获得字节数组 byte[] data = System.Text.Encoding.Default.GetBytes(" "+ str_time +" "+ str_msg); //开始写入 fs.Write(data, 0, data.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); }