Beispiel #1
0
 private static EmailMessage getErrorEmailMessage(string body)
 {
     return(new EmailMessage
     {
         Subject =
             "Error in {0}".FormatWith(ConfigurationStatics.InstallationConfiguration.SystemName) +
             (ConfigurationStatics.IsClientSideProgram ? " on {0}".FormatWith(StandardLibraryMethods.GetLocalHostName()) : ""),
         BodyHtml = body.GetTextAsEncodedHtml()
     });
 }
        /// <summary>
        /// Sends an error email message to the developer addresses specified in the config file using the SMTP server specified in the config file.
        /// </summary>
        private static void sendErrorEmail(string body)
        {
            assertClassInitialized();

            var m = new EmailMessage();

            foreach (var developer in ConfigurationStatics.InstallationConfiguration.Developers)
            {
                m.ToAddresses.Add(new EmailAddress(developer.EmailAddress, developer.Name));
            }
            m.Subject = "Error in " + ConfigurationStatics.InstallationConfiguration.SystemName;
            if (ConfigurationStatics.IsClientSideProgram)
            {
                m.Subject += " on " + StandardLibraryMethods.GetLocalHostName();
            }
            m.BodyHtml = body.GetTextAsEncodedHtml();
            SendEmailWithDefaultFromAddress(m);
        }
        /// <summary>
        /// Sends an email from the default address to the developers with the given exception information and additional information about
        /// the running program.  Prefix provides additional information before the standard exception and page information.
        /// The exception may be null.
        /// </summary>
        public static void EmailAndLogError(string prefix, Exception exception)
        {
            using (var sw = new StringWriter()) {
                if (prefix.Length > 0)
                {
                    sw.WriteLine(prefix);
                    sw.WriteLine();
                }
                if (exception != null)
                {
                    sw.WriteLine(exception.ToString());
                    sw.WriteLine();
                }

                if (NetTools.IsWebApp())
                {
                    // This check ensures that there is an actual request, which is not the case during application initialization.
                    if (EwfApp.Instance != null && EwfApp.Instance.RequestState != null)
                    {
                        sw.WriteLine("URL: " + AppRequestState.Instance.Url);

                        sw.WriteLine();
                        foreach (string fieldName in HttpContext.Current.Request.Form)
                        {
                            sw.WriteLine("Form field " + fieldName + ": " + HttpContext.Current.Request.Form[fieldName]);
                        }

                        sw.WriteLine();
                        foreach (string cookieName in HttpContext.Current.Request.Cookies)
                        {
                            sw.WriteLine("Cookie " + cookieName + ": " + HttpContext.Current.Request.Cookies[cookieName].Value);
                        }

                        sw.WriteLine();
                        sw.WriteLine("User agent: " + HttpContext.Current.Request.GetUserAgent());
                        sw.WriteLine("Referrer: " + NetTools.ReferringUrl);

                        User user         = null;
                        User impersonator = null;

                        // exception-prone code
                        try {
                            user         = User;
                            impersonator = AppRequestState.Instance.ImpersonatorExists ? AppRequestState.Instance.ImpersonatorUser : null;
                        }
                        catch {}

                        if (user != null)
                        {
                            sw.WriteLine("User: {0}{1}".FormatWith(user.Email, impersonator != null ? " (impersonated by {0})".FormatWith(impersonator.Email) : ""));
                        }
                    }
                }
                else
                {
                    sw.WriteLine("Program: " + ConfigurationStatics.AppName);
                    sw.WriteLine("Version: " + ConfigurationStatics.AppAssembly.GetName().Version);
                    sw.WriteLine("Machine: " + StandardLibraryMethods.GetLocalHostName());
                }

                StandardLibraryMethods.CallEveryMethod(delegate { sendErrorEmail(sw.ToString()); }, delegate { logError(sw.ToString()); });
            }
        }