WriteLog日志记录

    public static class CommonLog
    {
        public static string path
        {
            get
            {
                string pt = HttpContext.Current.Server.MapPath("~/logs");
                if (!Directory.Exists(pt)) { Directory.CreateDirectory(pt); }
                return pt + "\\";
            }
        }

        public static void WriteLog(string msg)
        {
            try
            {
                string File = string.Format("log{0}.txt", DateTime.Now.ToString("yyyyMMdd"));
                using (StreamWriter writer = new StreamWriter(path + File, true, Encoding.UTF8))
                {
                    writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                    writer.WriteLine(msg);
                    writer.Flush();
                    writer.Close();
                }
            }
            catch (Exception ex) { }
        }
    }
//调用
CommonLog.WriteLog("记录错误信息");

 

posted @ 2019-11-20 02:47  全栈攻城师  阅读(403)  评论(0)    收藏  举报