简单写日志类
public class LogManager { // 写日志文件 private static void WriteLog(string sLogPath, string sFileURL, object oMessage) { try { //看文件夹是否存在 if (!System.IO.Directory.Exists(sLogPath)) { System.IO.Directory.CreateDirectory(sLogPath); } //设置要写入日志的属性 if (System.IO.File.Exists(sFileURL)) { System.IO.File.SetAttributes(sFileURL, FileAttributes.Normal); } FileStream fs = new FileStream(sFileURL, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("时间——" + DateTime.Now + ";日志信息——" + oMessage + "\n"); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); } catch (Exception ex) { } } /// <summary> /// 在执行文件目录下的“wh_Log”文件夹下增加一条文本信息(文件名为sFileName+当前日期) /// </summary> /// <param name="sFileName">日志名称</param> /// <param name="oMessage">日志内容</param> public static void PrintLog(string sFileName, object oMessage) { string logPath = @"C:\Mu_Log\"; string sURL = logPath + @"\" + sFileName + "_" + DateTime.Today.ToString("yyyy-MM-dd") + ".Log"; WriteLog(logPath, sURL, oMessage); } }