简单的记录临时日志代码

 

简单记录日志:

 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();
            }

        }

 

posted on 2020-01-19 14:45  荆棘人  阅读(260)  评论(0编辑  收藏  举报

导航