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") + "	" + message+ "
");
                sw.Flush();
                sw.Close();  //数据写入log.txt
            }                  
                
        }

    }

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

AppDomain.CurrentDomain.BaseDirectory

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

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

  

原文地址:https://www.cnblogs.com/yunquan/p/7649121.html