创建日志文件类

    public class WriteLog
    {
        public static void WriteLogInfo(SqlException ex,string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\" + DateTime.Now.ToShortDateString() + ".log";
            WriteLogInfo(ex, path);
        }
        public static void WriteLogInfo(Exception ex,string path)
        {
            using(StreamWriter sw=new StreamWriter(path, true, Encoding.Default))
            {
                sw.WriteLine("***[" + DateTime.Now.ToShortDateString() + "]***");
                if (ex != null)
                {
                    sw.Write(ex.Message);
                }
                else
                {
                    sw.WriteLine("Exception is null");
                }
                sw.WriteLine();
            }
        }
        /// <summary>
        /// 创建日志文件
        /// </summary>
        /// <param name="ex"></param>
        public static void CreateLog(Exception ex)
        {
            //string AppName=Assembly.GetExecutingAssembly().GetName().CodeBase();
            //string path=Path.GetDirectoryName(AppName)+"\\log";
            string path = Application.StartupPath + "\\log";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);//创建日志文件夹
                path += "\\" + DateTime.Now.ToShortDateString() + ".log";
                WriteLogInfo(ex, path);
            }
        }
        public static void CreateLog(SqlException ex)
        {
            string path = Application.StartupPath + "\\log";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\" + DateTime.Now.ToShortDateString() + ".log";
            WriteLogInfo(ex, path);
        }
        
    }

 

posted @ 2020-09-05 22:25  tiger_yj  阅读(152)  评论(0编辑  收藏  举报