通常都用log4net
public static void WriteLog(string msg, string logName, string path) { var Log = ""; Log += msg + " "; if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } if (!System.IO.File.Exists(path + "/" + logName + "_Log" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt")) { FileStream fs1 = new FileStream(path + "/" + logName + "_Log" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt", FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs1); sw.WriteLine(Log); sw.Close(); fs1.Close(); } else { FileStream fs = new FileStream(path + "/" + logName + "_Log" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt" + "", FileMode.Append, FileAccess.Write); StreamWriter sr = new StreamWriter(fs); sr.WriteLine(Log); sr.Close(); fs.Close(); } }
一天生成一个,根据名字可生成不同的日志。