C# 一些代码小结--使用文件记录日志

C# 一些代码小结--使用文件记录日志

public class FaceLog
    {
        public static void AppendInfoLog(string errMsg)
        {
            try
            {
                string Folder = ".\\log\\";
                string fileName = Folder + "Info_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
               if (!System.IO.Directory.Exists(Folder))
                    System.IO.Directory.CreateDirectory(Folder);
                if (!File.Exists(fileName))
                {
                    FileStream stream = System.IO.File.Create(fileName);
                    stream.Close();
                    stream.Dispose();
                }
                using (TextWriter fs = new StreamWriter(fileName, true))
                {
                    fs.Write(errMsg);
                    fs.Close();
                    fs.Dispose();
                }
            }
            catch
            {
            }
        }
        public static void AppendSerialLog(string errMsg)
        {
            try
            {
                string Folder = ".\\log\\";
                string fileName = Folder + "Serial_" + 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.Write(errMsg);
                    fs.Close();
                    fs.Dispose();
                }
            }
            catch
            {
            }
        }
    }

声明:该内容转自:https://www.cnblogs.com/wangboke/p/5854144.html

posted @ 2019-03-22 15:11  MemoryPro  阅读(1183)  评论(0编辑  收藏  举报