C# Log日志

        //自动创建年月日时分秒日志文件,使用于一次性写入日志
        static public void WriteLog(string info) {//info:日志信息
            var path = AppDomain.CurrentDomain.BaseDirectory;
            path = System.IO.Path.Combine(path, "Data Log");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            using (var stream = new StreamWriter(path + "\\" + DateTime.Now.Year.ToString("0000") + "-" + DateTime.Now.Month.ToString("00") + "-" +
                DateTime.Now.Day.ToString("00") + " " + DateTime.Now.Hour.ToString("00") + "" + DateTime.Now.Minute.ToString("00") + ""
                + DateTime.Now.Second.ToString("00") + ".log", true)) {
                stream.WriteLine($"{info}");
            }
        }

        //向指定日志文件中追加日志
        static public void WriteLog(string path, string info) {//path: 日志文件 info:日志信息
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            using (var stream = new StreamWriter(path+".log", true)) {
                stream.WriteLine($"{info}");
            }
        }

 

posted @ 2022-11-14 11:29  阿坦  阅读(162)  评论(0)    收藏  举报