Ejemplo n.º 1
0
        protected void Application_Error(object sender, EventArgs e)
        {
            OnCustomEndRequest(sender, e);
            try
            {
                if (Context != null && Context.AllErrors != null)
                {
                    WebInitializer.Initialize();
                    var monitor = new LogMonitor();
                    monitor.Init();
                    var str2 = Tools.Security.User.GetSID();
                    var db   = new DBFilterValuesDataContext(SpecificInstances.DbFactory.CreateConnection());

                    // присвоение выбранного фильтра, для его пометки, как опасного
                    long?refUserFilter = null;
                    if (MainPageUrlBuilder.Current.IsDataControl &&
                        MainPageUrlBuilder.Current.UserControl != null &&
                        MainPageUrlBuilder.Current.UserControl.EndsWith("Journal"))
                    {
                        var tableName    = MainPageUrlBuilder.Current.UserControl.Substring(0, MainPageUrlBuilder.Current.UserControl.Length - 7);
                        var filterValues = MainPageUrlBuilder.Current.GetFilterItemsDic(tableName);
                        if (filterValues != null && filterValues.ContainsKey("__refUserFilterValues") &&
                            filterValues["__refUserFilterValues"].Count > 0 &&
                            !string.IsNullOrEmpty(filterValues["__refUserFilterValues"][0].Value1))
                        {
                            refUserFilter = Convert.ToInt64(filterValues["__refUserFilterValues"][0].Value1);
                        }
                    }

                    foreach (var exception in Context.AllErrors)
                    {
                        var message = string.Format("{0}: {1}", Context.Request.Url.PathAndQuery, exception);
                        var entry2  = new LogMessageEntry(LogMessageType.SystemErrorInApp, message)
                        {
                            Sid = str2
                        };
                        var logMessageEntry = entry2;
                        var refLog          = monitor.WriteLog(logMessageEntry);
                        // для ошибок дополнительно залогируем агрументы
                        if (Context.Request.HttpMethod.ToLower() == "post" && refLog != null)
                        {
                            foreach (string key in Context.Request.Form.Keys)
                            {
                                monitor.WriteFieldChanged(refLog.Value, "", key, Context.Request.Form[key], null);
                            }
                        }

                        var sqlException = exception as SqlException ?? exception.InnerException as SqlException;
                        if (sqlException != null && refUserFilter != null)
                        {
                            foreach (SqlError sqlError in sqlException.Errors)
                            {
                                if (sqlError.Number == -2)
                                {
                                    db.SYS_SetIsDangerousUserFilter(refUserFilter);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Ejemplo n.º 2
0
        public static void SendMail(
            string currentMailAddress,
            string html,
            string subject,
            IEnumerable <string> listEmails,
            IEnumerable <string> listEmailsCopy,
            IEnumerable <Attachment> attachments,
            string sid,
            bool throwException)
        {
            SmtpSection mailSettings;

            if (SpecificInstances.DbFactory == null)
            {
                mailSettings = (SmtpSection)WebConfigurationManager.GetSection("system.net/mailSettings/smtp");
            }
            else
            {
                mailSettings =
                    (SmtpSection)
                    ((IWebConfiguration)SpecificInstances.DbFactory).WebConfiguration.GetSection(
                        "system.net/mailSettings/smtp");
            }

            var    mailServer = WebConfigurationManager.AppSettings["MailServer"];
            string emailFrom;

            if (mailSettings != null && !string.IsNullOrEmpty(mailSettings.From))
            {
                emailFrom = mailSettings.From;
            }
            else
            {
                emailFrom = currentMailAddress;
            }

            if (string.IsNullOrEmpty(emailFrom))
            {
                throw new Exception("Not set emailFrom, e-mail can't be sended");
            }

            var mm = new MailMessage
            {
                From    = new MailAddress(emailFrom),
                Subject = subject,
                //SubjectEncoding = Encoding.UTF8,
                Body       = html,
                IsBodyHtml = true,
            };

            foreach (var email in listEmails.Where(r => !string.IsNullOrEmpty(r)))
            {
                mm.To.Add(new MailAddress(email));
            }

            if (listEmailsCopy != null)
            {
                foreach (var email in listEmailsCopy.Where(r => !string.IsNullOrEmpty(r)))
                {
                    mm.CC.Add(new MailAddress(email));
                }
            }

            SmtpClient client;

            if (mailSettings == null || string.IsNullOrEmpty(mailSettings.From))
            {
                client = new SmtpClient(mailServer, 25)
                {
                    Credentials = CredentialCache.DefaultNetworkCredentials,
                }
            }
            ;
            else
            {
                var credentails = new NetworkCredential(
                    mailSettings.Network.UserName,
                    mailSettings.Network.Password,
                    mailSettings.Network.ClientDomain);
                client = new SmtpClient(mailSettings.Network.Host, mailSettings.Network.Port)
                {
                    Credentials           = credentails,
                    UseDefaultCredentials = mailSettings.Network.DefaultCredentials,
                    EnableSsl             = Convert.ToBoolean(WebConfigurationManager.AppSettings["Email_EnableSsl"]),
                };

                client.Credentials = credentails;
            }

            if (attachments != null)
            {
                foreach (var attachment in attachments)
                {
                    mm.Attachments.Add(attachment);
                }
            }

            var monitor = new LogMonitor();

            monitor.Init();
            try
            {
                monitor.Log(
                    LogMessageType.SystemMailSendMailInformation,
                    () =>
                {
                    var toMailsArray = mm.To.Select(
                        r => string.IsNullOrEmpty(r.DisplayName) ? r.Address : r.DisplayName).ToArray();
                    var toMails   = string.Join("; ", toMailsArray);
                    var fromEmail = string.IsNullOrEmpty(mm.From.DisplayName)
                                                ? mm.From.Address
                                                : mm.From.DisplayName;
                    var message = string.Format(
                        "От: {0}; \r\nКому: {1}; \r\nТема: {2}",
                        fromEmail,
                        toMails,
                        mm.Subject);
                    monitor.FieldChanged("EMailBody", "Содержание", "", mm.Body);
                    return(new LogMessageEntry(sid, LogMessageType.SystemMailSendMailInformation, message));
                });
                client.Send(mm);
            }
            catch (Exception e)
            {
                var messageEntry = new LogMessageEntry(sid, LogMessageType.SystemMailSendMailError, e.ToString());
                monitor.Log(messageEntry);
                if (throwException)
                {
                    throw;
                }
            }
        }