简单的记录临时日志代码
简单记录日志:
private void WriteLog(string jsonStr) { File.AppendAllText("D:\\log.txt", DateTime.Now.ToString() + "--" + jsonStr + Environment.NewLine); }
复杂版:
public static void WriteLog(string msg) { string baseDir = AppDomain.CurrentDomain.BaseDirectory; string fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; string savePath = baseDir + fileName; msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\tInfo:" + msg + "\r\n"; if (File.Exists(savePath)) { using (FileStream fs = File.Open(savePath, FileMode.Append)) { byte[] data = Encoding.UTF8.GetBytes(msg); fs.Write(data, 0, data.Length); } } else { FileStream fs = File.Create(savePath); byte[] data = Encoding.UTF8.GetBytes(msg); fs.Write(data, 0, data.Length); fs.Close(); } }