C# 日志

通常都用log4net

public static void WriteLog(string msg, string logName, string path)
        {
            var Log = "";
            Log += msg + "\r\n";

            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
           
            if (!System.IO.File.Exists(path + "/" + logName + "_Log" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt"))
            {
                FileStream fs1 = new FileStream(path + "/" + logName + "_Log" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt", FileMode.Create, FileAccess.Write); 
                StreamWriter sw = new StreamWriter(fs1);
                sw.WriteLine(Log);
                sw.Close();
                fs1.Close();
            }
            else
            {
                FileStream fs = new FileStream(path + "/" + logName + "_Log" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt" + "", FileMode.Append, FileAccess.Write);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(Log);
                sr.Close();
                fs.Close();
            }
        }

一天生成一个,根据名字可生成不同的日志。

 

posted @ 2019-02-13 16:45  liuyong111  阅读(252)  评论(0编辑  收藏  举报