private void WriteLog(string getdata, string logMessage) { string strPath = null; FileStream fs = null; StreamWriter m_streamWriter = null; string logfolder = Path.GetDirectoryName(Application.ExecutablePath) + @"\log"; if(!Directory.Exists(logfolder)) { Directory.CreateDirectory(logfolder); } string logfilename = System.DateTime.Now.ToString("yyyyMMdd"); strPath = logfolder + "\\"+logfilename + ".txt"; fs = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write); m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("--------{0} {1}----begin-----", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()); m_streamWriter.WriteLine("接收到的数据:\r\n"+getdata); m_streamWriter.WriteLine("操作结果:"+logMessage); m_streamWriter.WriteLine("----end-----"); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); }
添加日志文件
关键字词: