博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

txt文本文件记录日志

Posted on 2018-02-19 19:44  火冰·瓶  阅读(143)  评论(0编辑  收藏  举报
        private static void Log(string content, string fileName="log.txt")
        {
            string logsPath = AppDomain.CurrentDomain.BaseDirectory + "\\App_Code\\logs\\";
            string filename = logsPath + fileName;
            if (!Directory.Exists(logsPath))
                Directory.CreateDirectory(logsPath);
            StreamWriter sr = null;
            try
            {
                if (!File.Exists(filename))
                {
                    sr = File.CreateText(filename);
                }
                else
                {
                    sr = File.AppendText(filename);
                }
                sr.WriteLine(content);
            }
            catch
            {
            }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
        }