Esempio n. 1
0
        public static void WriteLogFileAndEmail(UnhandledExceptionEventArgs e)
        {
            var    ex = e.ExceptionObject as Exception;
            string exceptionContent = null;

            if (ex != null)
            {
                if (ex.Message.Contains("信息已过期"))
                {
                    return;
                }
                StackTrace st = new StackTrace(ex, true);
                exceptionContent = string.Format("错误的信息:{0}\n出错的方法名:{1}\n出错的类名:{2}\n出错行号:{3}\n文件名:{4}\n错误的堆栈{5}",
                                                 ex.Message, ex.TargetSite.Name, ex.GetType().Name, st.GetFrame(0).GetFileLineNumber(), st.GetFrame(0).GetFileName(), ex.StackTrace);
                //exceptionContent = ex.Message.Equals(SQLError.ErrorValue.UpdatedError) ? ex.Message : string.Format("Application UnhandledException:{0};\n\r堆栈信息:{1}", ex.Message, ex.StackTrace);
            }

            if (!Directory.Exists(Application.StartupPath + @"\ErrorLog"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"\ErrorLog");
            }
            string errLogFile = Application.StartupPath + @"\ErrorLog\Exception" + BLLCom.GetCurStdDatetime().ToString("yyyyMMdd") + "_" + Guid.NewGuid().ToString().ToUpper() + ".txt";

            using (StreamWriter sw = new StreamWriter(errLogFile, true))
            {
                sw.WriteLine(exceptionContent);
                sw.WriteLine("---------------------------------------------------------");
                sw.Close();
            }
            string messageTitle  = (ex != null ? ex.Message : string.Empty);
            bool   ignoreMessage = IgnoreExceptionsMessage.Any(ignoreMsg => messageTitle.Contains(ignoreMsg));

            if (!ignoreMessage && !string.IsNullOrEmpty(SystemConfigInfo.SkyCarDevEmailAdress))
            {
                string emailTitle = LoginInfoDAX.SPCode + "(" + SysConst.VersionNo + ")" + "在" + LoginInfoDAX.MCTCode + "_" + LoginInfoDAX.OrgShortName + "_" + "出现异常:" + (ex != null ? ex.Message : string.Empty) + ",物理内存:" + LocalSystemHelper.GetPhisicalMemorySize() + "G,可用内存:" + LocalSystemHelper.AvailableMemory + "G";
                if (!SameContentFileExists(Application.StartupPath + @"\ErrorLog", errLogFile, true))
                {
                    BLLCom.SendMail(SystemConfigInfo.SkyCarDevEmailAdress, string.Empty, "异常反馈:" + emailTitle, LoginInfoDAX.UserID + "在" + LoginInfoDAX.OrgShortName + "发现了异常,详情见附件。", errLogFile);
                }
            }
        }