0
点赞
收藏
分享

微信扫一扫

C# 保存log Demo

yundejia 2022-04-08 阅读 208
        /// cmd指令:tree /f >list.txt 
        /// 目录结构 
        ///└─Log
        ///    └─20220408
        ///            test1.log

        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="info">信息内容</param>
        /// <param name="str">文件名</param>
        public static void WriteLog(string info, string str)
        {
            string logDateDirPath = @"./Log/" + DateTime.Now.ToString("yyyyMMdd");

            string logFileName = logDateDirPath + "\\" + str + ".log"; ;

            if (!Directory.Exists(logDateDirPath))
            {
                Directory.CreateDirectory(logDateDirPath);
            }

            try
            {
                TextWriter tw = new StreamWriter(logFileName, true, System.Text.Encoding.Default); ;

                tw.WriteLine(info);

                tw.Close();
            }
            catch (Exception ex)
            {

            }
        }
举报

相关推荐

0 条评论