internal static void HandleError(HttpApplication app, Exception exception, bool HandledByUser) { try { Tenor.Configuration.Tenor config = Tenor.Configuration.Tenor.Current; if (exception == null) { throw new ArgumentNullException("exception"); } if (config.Exceptions.LogMode == LogMode.None || (config.Exceptions.LogMode == LogMode.Email && config.Exceptions.Emails.Count == 0) || (config.Exceptions.LogMode == LogMode.File && (string.IsNullOrEmpty(config.Exceptions.FilePath) || !Directory.Exists(config.Exceptions.FilePath)))) { return; } string requestUrl = string.Empty; if (app != null) { requestUrl = app.Request.Url.GetLeftPart(UriPartial.Authority) + app.Request.RawUrl; } string title = (HandledByUser ? "Handled" : "Unhandled").ToString() + " Exception: " + requestUrl + " (" + DateTime.Now.ToString() + ")"; string body = string.Empty; try { HttpException htmlex = exception as HttpException; if (htmlex != null) { body = htmlex.GetHtmlErrorMessage(); } } catch { } if (body.Trim().Length < 10) { body = BuildExceptionDetails(app, exception); } body = BuildExtraInfo(app, exception, body); if (config.Exceptions.LogMode == LogMode.Email) { Mail.MailMessage errmail = new Mail.MailMessage(); foreach (EmailElement email in config.Exceptions.Emails) { errmail.To.Add(new System.Net.Mail.MailAddress(email.Email, email.Name)); } if (errmail.To.Count == 0) { errmail.Dispose(); return; } errmail.Subject = title; errmail.IsBodyHtml = true; errmail.BodyEncoding = System.Text.Encoding.UTF8; errmail.Body = body; errmail.Send(); } else if (config.Exceptions.LogMode == LogMode.File) { string path = config.Exceptions.FilePath; if (!path.EndsWith("\\")) { path += "\\"; } File.WriteAllText(string.Format("{1}\\{0:yyyy-MM-dd-HH-mm-ss}.html", DateTime.Now, path), body, System.Text.Encoding.UTF8); } } catch { } }
protected void SendMessage() { try { string nome = string.Empty; string email = string.Empty; Control ctrlNome = FormView.NamingContainer.FindControl(FieldPrefix + "Nome"); Control ctrlEmail = FormView.NamingContainer.FindControl(FieldPrefix + "Email"); if (ctrlNome.GetType() == typeof(System.Web.UI.WebControls.TextBox)) { nome = ((System.Web.UI.WebControls.TextBox)ctrlNome).Text.Trim(); } else if (ctrlNome.GetType() == typeof(TextBox)) { nome = ((TextBox)ctrlNome).Text.Trim(); } else if (ctrlNome.GetType() == typeof(HiddenField)) { nome = ((HiddenField)ctrlNome).Value.Trim(); } if (ctrlEmail.GetType() == typeof(System.Web.UI.WebControls.TextBox)) { email = ((System.Web.UI.WebControls.TextBox)ctrlEmail).Text.Trim(); } else if (ctrlEmail.GetType() == typeof(TextBox)) { email = ((TextBox)ctrlEmail).Text.Trim(); } else if (ctrlEmail.GetType() == typeof(HiddenField)) { email = ((HiddenField)ctrlEmail).Value.Trim(); } Mail.MailMessage msg = new Mail.MailMessage(); if (!string.IsNullOrEmpty(TemplateUrl)) { msg.Template = System.IO.File.ReadAllText(Page.MapPath(TemplateUrl)); } msg.From = new System.Net.Mail.MailAddress(EmailFrom, nome); msg.ReplyTo = new System.Net.Mail.MailAddress(email, nome); string template = string.Empty; BuildTemplate(ref msg, ref template, FormView.Controls); string mensagem = string.Empty; System.Collections.Specialized.NameValueCollection dados = new System.Collections.Specialized.NameValueCollection(); foreach (string key in msg.TemplateValues.Keys) { dados.Add(key, msg.TemplateValues[key]); if (msg.TemplateValues[key].Length > Tenor.Configuration.MailMessage.MaxLengthTemplateValue) { mensagem += "- O campo " + key + " só pode ter até " + Tenor.Configuration.MailMessage.MaxLengthTemplateValue.ToString() + " caracteres." + Environment.NewLine; } } msg.To.Add(EmailTos); msg.Subject = EmailSubject; if (!string.IsNullOrEmpty(mensagem)) { Alert(mensagem); } else { if (string.IsNullOrEmpty(msg.Template)) { msg.Template = template; } msg.IsBodyHtml = true; msg.Send(); SetActiveView(ResultView); } } catch (Exception ex) { Alert(ErrorMessage); Diagnostics.Debug.HandleError(ex); System.Diagnostics.Debug.Print(ex.ToString()); } }