代码段——写日志文件

        /// <summary>
        /// 写入日志
        /// </summary>
        public void WriteLog(string message)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "/MyLog.txt";
            //using (StreamWriter sw = new StreamWriter(path, true, Encoding.Default))
            //{
            //    sw.Flush();
            //    sw.WriteLine("时间:" + DateTime.Now);
            //    sw.WriteLine("内容:" + message);
            //    sw.WriteLine("-------------------------");
            //}
            //这里比较适合记录JSON日志,即将日志对象序列为message
            File.AppendAllText(filePath, message + Environment.NewLine);
        }
posted @ 2023-03-23 16:32  shanzm  阅读(18)  评论(0)    收藏  举报
TOP