public class Log { static public void WriteTxt(string info) { var path = AppDomain.CurrentDomain.BaseDirectory; path = System.IO.Path.Combine(path, "Data Log"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); using (var stream = new StreamWriter(path + "\\" + DateTime.Now.Year.ToString("0000") + "-" + DateTime.Now.Month.ToString("00") + "-" + DateTime.Now.Day.ToString("00") + " " + DateTime.Now.Hour.ToString("00") + "∶" + DateTime.Now.Minute.ToString("00") + "∶"//非英文冒号 + DateTime.Now.Second.ToString("00")+".txt", true)) { stream.WriteLine($"{info}"); } } static public void WriteCsv(string info) { var path = AppDomain.CurrentDomain.BaseDirectory; path = System.IO.Path.Combine(path, "Data Log"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); using (var stream = new StreamWriter(path + "\\" + DateTime.Now.Year.ToString("0000") + "-" + DateTime.Now.Month.ToString("00") + "-" + DateTime.Now.Day.ToString("00") + " " + DateTime.Now.Hour.ToString("00") + "∶" + DateTime.Now.Minute.ToString("00") + "∶" + DateTime.Now.Second.ToString("00") + ".csv", true)) { stream.WriteLine($"{info}"); } } static public void WriteLog(string info) { var path = AppDomain.CurrentDomain.BaseDirectory; path = System.IO.Path.Combine(path, "Data Log"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); using (var stream = new StreamWriter(path + "\\" + DateTime.Now.Year.ToString("0000") + "-" + DateTime.Now.Month.ToString("00") + "-" + DateTime.Now.Day.ToString("00") + " " + DateTime.Now.Hour.ToString("00") + "∶" + DateTime.Now.Minute.ToString("00") + "∶" + DateTime.Now.Second.ToString("00") + ".log", true)) { stream.WriteLine($"{info}"); } } }创建年月日时分秒文件夹,适用于一次性写入日志。
标签:00,Log,C#,DateTime,ToString,path,日志,Now From: https://www.cnblogs.com/lizhiqiang0204/p/16888476.html