C#写日志

 

  在做程序的时候,经常会用到写Log日志这个功能,大佬写的很是齐全,不过这个我就写一个最简单的吧...  

贴一个Log类:

  class Log
    {
        public static void Save(string message)
        {
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("" + AppDomain.CurrentDomain.BaseDirectory + "\\log.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + message+ "\r");
                sw.Flush();
                sw.Close();  //数据写入log.txt
            }                  
                
        }

    }

  上面的代码唯一的亮点就是

AppDomain.CurrentDomain.BaseDirectory

  这句代码的意思是找到程序的基目录,其实就是本程序的Debug目录下,更多的详情可以参考这篇文章

  双\\是\的意思,避免转义。

  

posted @ 2017-10-11 09:54  蜀云泉  阅读(315)  评论(0编辑  收藏  举报