C# 利用file打印日志

  public class FaceLog
    {
        public static void AppendInfoLog(string errMsg)
        {
            try
            {
                string Folder = Main.Instance.AppPath + "\Logs\";
                string fileName = Folder + "Info_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                if (!System.IO.Directory.Exists(Folder))
                    System.IO.Directory.CreateDirectory(Folder);
                if (!File.Exists(fileName))
                    System.IO.File.Create(fileName);
                using (TextWriter fs = new StreamWriter(fileName, true))
                {
                    fs.WriteLine("--------------------------" + DateTime.Now.ToString() + "----------------------------------------");
                    fs.WriteLine(errMsg);
                    fs.WriteLine("");
                    fs.WriteLine("");
                    fs.Close();
                    fs.Dispose();
                }
            }
            catch
            {
            }
        }
        public static void AppendErrorLog(string errMsg)
        {
            try
            {
                string Folder = Main.Instance.AppPath + "\Logs\";
                string fileName = Folder + "Error_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                if (!System.IO.Directory.Exists(Folder))
                    System.IO.Directory.CreateDirectory(Folder);
                if (!File.Exists(fileName))
                    System.IO.File.Create(fileName);
                using (TextWriter fs = new StreamWriter(fileName, true))
                {
                    fs.WriteLine("--------------------------" + DateTime.Now.ToString() + "----------------------------------------");
                    fs.WriteLine(errMsg);
                    fs.WriteLine("");
                    fs.WriteLine("");
                    fs.Close();
                    fs.Dispose();
                }
            }
            catch
            {
            }
        }
    }
原文地址:https://www.cnblogs.com/wangboke/p/5854144.html