/// <summary> /// Log an exception and send an email. /// </summary> /// <param name="ex"></param> /// <param name="nodeId"></param> /// <param name="type"></param> public static void LogException(this UmbracoHelper umbraco, Exception ex) { try { int nodeId = -1; if (System.Web.HttpContext.Current.Items["pageID"] != null) { int.TryParse(System.Web.HttpContext.Current.Items["pageID"].ToString(), out nodeId); } StringBuilder comment = new StringBuilder(); StringBuilder commentHtml = new StringBuilder(); commentHtml.AppendFormat("<p><strong>Url:</strong><br/>{0}</p>", System.Web.HttpContext.Current.Request.Url.AbsoluteUri); commentHtml.AppendFormat("<p><strong>Node id:</strong><br/>{0}</p>", nodeId); //Add the exception. comment.AppendFormat("Exception: {0} - StackTrace: {1}", ex.Message, ex.StackTrace); commentHtml.AppendFormat("<p><strong>Exception:</strong><br/>{0}</p>", ex.Message); commentHtml.AppendFormat("<p><strong>StackTrace:</strong><br/>{0}</p>", ex.StackTrace); if (ex.InnerException != null) { //Add the inner exception. comment.AppendFormat("- InnerException: {0} - InnerStackTrace: {1}", ex.InnerException.Message, ex.InnerException.StackTrace); commentHtml.AppendFormat("<p><strong>InnerException:</strong><br/>{0}</p>", ex.InnerException.Message); commentHtml.AppendFormat("<p><strong>InnerStackTrace:</strong><br/>{1}</p>", ex.InnerException.StackTrace); } //Log the Exception into Umbraco. Log.Add(LogTypes.Error, nodeId, comment.ToString()); //Send an email with the exception. umbraco.SendEmail(umbraco.Config().ErrorFrom, umbraco.Config().ErrorFromName, umbraco.Config().ErrorTo, "Error log", commentHtml.ToString()); } catch { //Do nothing because nothing can be done with this exception. } }