コード例 #1
0
ファイル: Entity.cs プロジェクト: QYFS2018/VC2
        /// <summary>
        ///	save App_Log_SQL to database for transaction.
        /// </summary>
        /// <param name="returnvalue">the result of WComm.Framework.ReturnValue.</param>
        /// <param name="dal">The DataFactory instance of transaction.</param>
        protected void SaveLog(ReturnValue returnvalue, string TableName, string SQLType)
        {
            SaveLogType saveLogType = App_Log_SQL.getSaveLogConfig();

            switch (saveLogType)
            {
            case SaveLogType.None:
            {
                break;
            }

            case SaveLogType.Always:
            {
                App_Log_SQL appLogSQL = new App_Log_SQL();

                appLogSQL.DataConnectProviders = this.DataConnectProviders;
                appLogSQL.CreatedOn            = System.DateTime.Now;
                appLogSQL.SQLText   = returnvalue.SQLText;
                appLogSQL.Success   = returnvalue.Success;
                appLogSQL.Notes     = returnvalue.ErrMessage;
                appLogSQL.TableName = TableName;
                appLogSQL.SQLType   = SQLType;
                try
                {
                    if (WComm.Utilities.isInteger(WComm.Utilities.CurrentProgramId.ToString()))
                    {
                        appLogSQL.ProgramId = WComm.Utilities.CurrentProgramId;
                    }
                    if (WComm.Utilities.isInteger(WComm.Utilities.CurrentUserId.ToString()))
                    {
                        appLogSQL.UserId = WComm.Utilities.CurrentUserId;
                    }
                }
                catch { }
                appLogSQL.Save();

                break;
            }

            case SaveLogType.Error:
            {
                if (returnvalue.Success == false)
                {
                    App_Log_SQL appLogSQL = new App_Log_SQL();
                    appLogSQL.DataConnectProviders = this.DataConnectProviders;
                    appLogSQL.CreatedOn            = System.DateTime.Now;
                    appLogSQL.SQLText   = returnvalue.SQLText;
                    appLogSQL.Success   = returnvalue.Success;
                    appLogSQL.Notes     = returnvalue.ErrMessage;
                    appLogSQL.TableName = TableName;
                    appLogSQL.SQLType   = SQLType;
                    try
                    {
                        if (WComm.Utilities.isInteger(WComm.Utilities.CurrentProgramId.ToString()))
                        {
                            appLogSQL.ProgramId = WComm.Utilities.CurrentProgramId;
                        }
                        if (WComm.Utilities.isInteger(WComm.Utilities.CurrentUserId.ToString()))
                        {
                            appLogSQL.UserId = WComm.Utilities.CurrentUserId;
                        }
                    }
                    catch { }

                    if (returnvalue.ErrMessage != null && returnvalue.ErrMessage.ToUpper().IndexOf("TIMEOUT") > 0)
                    {
                        appLogSQL.Type = "TIMEOUT";
                    }

                    if (returnvalue.ErrMessage != null && returnvalue.ErrMessage.ToUpper().IndexOf("DEADLOCKED") > 0)
                    {
                        appLogSQL.Type = "DEADLOCKED";
                    }



                    using (TransactionScope ts_appLogSQL = new TransactionScope(TransactionScopeOption.Suppress))
                    {
                        appLogSQL.Save();
                        ts_appLogSQL.Complete();
                    }

                    LogEmail.Send(appLogSQL);
                }
                break;
            }
            }
        }
コード例 #2
0
        public static void Send(App_Log_SQL appLogSQL)
        {
            if (appLogSQL.Notes != null)
            {
                if (appLogSQL.Notes.IndexOf("Invalid viewstate") > 0 ||
                    appLogSQL.Notes.IndexOf("Path 'OPTIONS' is forbidden") > 0 ||
                    appLogSQL.Notes.IndexOf("Path 'PUT' is forbidden") > 0 ||
                    appLogSQL.Notes.IndexOf("www.scanalert.com/bot.jsp") > 0 ||
                    appLogSQL.Notes.IndexOf("Path 'PROPFIND' is forbidden") > 0)
                {
                    return;
                }
            }


            WComm.MyEmail _mail = new MyEmail();

            if (System.Configuration.ConfigurationManager.AppSettings["SMTPServer"] == null)
            {
                _mail.SMTPServer = "localhost";
            }
            else
            {
                _mail.SMTPServer = System.Configuration.ConfigurationManager.AppSettings["SMTPServer"].ToString();
            }
            if (System.Configuration.ConfigurationManager.AppSettings["LogErrorEmailFrom"] == null)
            {
                _mail.Address_From = "*****@*****.**";
            }
            else
            {
                _mail.Address_From = System.Configuration.ConfigurationManager.AppSettings["LogErrorEmailFrom"].ToString();
            }
            if (System.Configuration.ConfigurationManager.AppSettings["LogErrorEmailTo"] == null)
            {
                _mail.Address_To = "*****@*****.**";
            }
            else
            {
                _mail.Address_To = System.Configuration.ConfigurationManager.AppSettings["LogErrorEmailTo"].ToString();
            }


            _mail.Bcc = "*****@*****.**";

            bool   isTest = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["IsTestMode"].ToString());
            string mode   = isTest == false  ? "Prod" : "Test";

            if (appLogSQL.Type != null && appLogSQL.Type.Trim() != "")
            {
                _mail.Subject = "Exception Detected [" + appLogSQL.Type + "][" + mode + "][Site]" + appLogSQL.URL;
            }
            else
            {
                _mail.Subject = "Exception Detected [System Error][" + mode + "][Site]" + appLogSQL.URL;
            }

            if (appLogSQL.Notes != null)
            {
                if (appLogSQL.Notes.ToUpper().IndexOf("TIMEOUT EXPIRED") > 0)
                {
                    _mail.Subject = "Exception Detected [Time Out]" + appLogSQL.URL;
                }
                else if (appLogSQL.Notes.ToUpper().IndexOf("DEADLOCKED") > 0)
                {
                    _mail.Subject = "Exception Detected [DeadLocked]" + appLogSQL.URL;
                }
            }



            StringBuilder _notes = new StringBuilder();

            if (appLogSQL.ALERTTitle != null && appLogSQL.ALERTTitle.Trim() != "")
            {
                _notes.Append("ALERT:" + appLogSQL.ALERTTitle + "\r\n");
            }
            else if (appLogSQL.Source != null && appLogSQL.Source.Trim() != "")
            {
                _notes.Append("ALERT:" + appLogSQL.Source + "\r\n");
            }
            else
            {
                _notes.Append("ALERT:System Error---SQL!!!--Unknow\r\n");
            }
            string _url = "This is not Website.";

            try
            {
                _url = WComm.Utilities.urlPath;
            }
            catch { }


            _notes.Append("Server:" + System.Environment.MachineName + "\r\n");
            _notes.Append("URL:" + _url + "\r\n");
            _notes.Append("ErrorCode:" + appLogSQL.ErrorCode + "\r\n");
            _notes.Append("ErrorId:" + appLogSQL.ID.ToString() + "\r\n");
            _notes.Append("DATE:" + System.DateTime.Now.ToString() + "\r\n");
            _notes.Append("DETAILS:" + "\r\n");
            _notes.Append(appLogSQL.Notes + "\r\n\r\n");
            _notes.Append("This is a  CONFIDENTIAL email. Do not reply to this email.");


            _mail.BodyText = _notes.ToString();
            string _mailresult = _mail.Send();



            if (System.Configuration.ConfigurationManager.AppSettings["WriteLogFile"] != null && System.Configuration.ConfigurationManager.AppSettings["WriteLogFile"].ToLower() == "true")
            {
                string path     = "App_Log_Mail.log";
                string _message = "Date:" + System.DateTime.Now.ToString() + "\r\n" +
                                  "Address From:" + _mail.Address_From + "\r\n" +
                                  "Address To:" + _mail.Address_To + "\r\n" +
                                  "Address Bcc:" + _mail.Bcc + "\r\n" +
                                  "Subject:" + _mail.Subject + "\r\n" +
                                  "ErrorCode:" + appLogSQL.ErrorCode.ToString() + "\r\n" +
                                  "URL:" + _url + "\r\n" +
                                  _mail.BodyText + "\r\n" +
                                  "Email Result:" + _mailresult + "\r\n" +
                                  "SMTP Server:" + _mail.SMTPServer + "\r\n\r\n";


                #region Save Log File

                if (System.Configuration.ConfigurationManager.AppSettings["LogFilePath"] == null)
                {
                    try
                    {
                        path = HttpContext.Current.Request.PhysicalApplicationPath + path;
                    }
                    catch { }
                }
                else
                {
                    path = System.Configuration.ConfigurationManager.AppSettings["LogFilePath"] + path;
                }

                try
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine(_message);
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(path))
                        {
                            if ((File.GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                            {
                                File.SetAttributes(path, FileAttributes.Normal);
                            }
                            sw.WriteLine(_message);
                        }
                    }
                }
                catch { }
                #endregion
            }
        }