public ErrorLog Create(string applicationId) { var config = new Dictionary<string, string> { { "applicationName", applicationId }, { "connectionStringName", _connectionStringName } }; var log = new SqlErrorLog(config); return log; }
/// <summary> /// Prepared for integration tests /// </summary> private static void ErrorLogForPredefinedConnectionString(Exception ex) { var log = new SqlErrorLog(ConnectionString); log.ApplicationName = ElmahApplicationName; log.Log(new Error(ex)); }
public ActionResult QueueEmails(MassEmailer m) { m.Body = GetBody(m.Body); if (UsesGrammarly(m)) return Json(new { error = GrammarlyNotAllowed }); if (TooLarge(m)) return Json(new { error = TooLargeError }); if (!m.Subject.HasValue() || !m.Body.HasValue()) return Json(new { id = 0, error = "Both subject and body need some text." }); if (!User.IsInRole("Admin") && m.Body.Contains("{createaccount}", ignoreCase: true)) return Json(new { id = 0, error = "Only Admin can use {createaccount}." }); if (Util.SessionTimedOut()) { Session["massemailer"] = m; return Content("timeout"); } DbUtil.LogActivity("Emailing people"); if (m.EmailFroms().Count(ef => ef.Value == m.FromAddress) == 0) return Json(new { id = 0, error = "No email address to send from." }); m.FromName = m.EmailFroms().First(ef => ef.Value == m.FromAddress).Text; int id; try { var eq = m.CreateQueue(); if (eq == null) throw new Exception("No Emails to send (tag does not exist)"); id = eq.Id; // If there are additional recipients, add them to the queue if (m.AdditionalRecipients != null) { foreach (var pid in m.AdditionalRecipients) { // Protect against duplicate PeopleIDs ending up in the queue var q3 = from eqt in DbUtil.Db.EmailQueueTos where eqt.EmailQueue == eq where eqt.PeopleId == pid select eqt; if (q3.Any()) { continue; } eq.EmailQueueTos.Add(new EmailQueueTo { PeopleId = pid, OrgId = eq.SendFromOrgId, Guid = Guid.NewGuid(), }); } DbUtil.Db.SubmitChanges(); } if (eq.SendWhen.HasValue) return Json(new { id = 0, content = "Emails queued to be sent." }); } catch (Exception ex) { ErrorSignal.FromCurrentContext().Raise(ex); return Json(new { id = 0, error = ex.Message }); } var host = Util.Host; // save these from HttpContext to set again inside thread local storage var userEmail = Util.UserEmail; var isInRoleEmailTest = User.IsInRole("EmailTest"); var isMyDataUser = User.IsInRole("Access") == false; try { ValidateEmailReplacementCodes(DbUtil.Db, m.Body, new MailAddress(m.FromAddress)); } catch (Exception ex) { return Json(new { error = ex.Message }); } HostingEnvironment.QueueBackgroundWorkItem(ct => { try { var db = DbUtil.Create(host); var cul = db.Setting("Culture", "en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cul); // set these again inside thread local storage Util.UserEmail = userEmail; Util.IsInRoleEmailTest = isInRoleEmailTest; Util.IsMyDataUser = isMyDataUser; db.SendPeopleEmail(id); } catch (Exception ex) { var ex2 = new Exception($"Emailing error for queueid {id} on {host}\n{ex.Message}", ex); var cb = new SqlConnectionStringBuilder(Util.ConnectionString) {InitialCatalog = "ELMAH"}; var errorLog = new SqlErrorLog(cb.ConnectionString) {ApplicationName = "BVCMS"}; errorLog.Log(new Error(ex2)); var db = DbUtil.Create(host); var equeue = db.EmailQueues.Single(ee => ee.Id == id); equeue.Error = ex.Message.Truncate(200); db.SubmitChanges(); } }); return Json(new { id = id }); }