ErrorLog日志记录类

 public class ErrorLog 
    { 
        /**////  
        /// 将错误以文本的形式记录下来. 
        ///  
        ///  
        public static void WriteLog(Exception Ex) 
        { 
            string ErrTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 
            string ErrSource = Ex.Source; 
            string ErrTargetSite = Ex.TargetSite.ToString(); 
            string ErrMsg = Ex.Message; 
            string ErrStackTrace = Ex.StackTrace; 
            string FilePath = System.Web.HttpContext.Current.Server.MapPath("\\DataBase Error Report\\"); 
            if (!Directory.Exists(FilePath)) 
            { 
                Directory.CreateDirectory(FilePath); 
            } 
            string FileName = FilePath + DateTime.Now.ToString("yyyy-MM-dd") + ".log"; 
 
            StreamWriter MySw = new StreamWriter(FileName, true, Encoding.UTF8); 
            MySw.WriteLine("错误时间 : " + ErrTime); 
            MySw.WriteLine("错误对象 : " + ErrSource); 
            MySw.WriteLine("异常方法 : " + ErrTargetSite); 
            MySw.WriteLine("错误信息 : " + ErrMsg); 
            MySw.WriteLine("堆栈内容 : "); 
            MySw.WriteLine(ErrStackTrace); 
            MySw.WriteLine("\r\n*****************Qindeke*Error*Report*****************\r\n"); 
            MySw.Close(); 
            MySw.Dispose(); 
 
        } 
 
        private static long GetFileSize(string FileName) 
        { 
            long strRe = 0; 
            if (File.Exists(FileName)) 
            { 
                FileStream MyFs = new FileStream(FileName, FileMode.Open); 
                strRe = MyFs.Length / 1024; 
                MyFs.Close(); 
                MyFs.Dispose(); 
            } 
            return strRe; 
        } 
 
        private static void CopyToBak(string sFileName) 
        { 
            int FileCount = 0; 
            string sBakName = ""; 
            do 
            { 
                FileCount++; 
                sBakName = sFileName + "." + FileCount.ToString() + ".BAK"; 
            } 
            while (File.Exists(sBakName)); 
            File.Copy(sFileName, sBakName); 
            File.Delete(sFileName); 
        } 
    }
 


 

posted on 2010-08-31 17:37  才君  阅读(322)  评论(0编辑  收藏  举报

导航