//public static bool SetCoreConfigSettingValue(string strKey, string strValue) //{ // throw (new NotImplementedException()); //} private static void WriteMessage(string message, Exception exception, bool sendEmail) { var mLog = new StringBuilder(); mLog.Append(message); mLog.Append(CharSeparate); AdvException currError = null; if (exception != null) { mLog.AppendFormat("\"{0}\";", exception.Message); currError = new AdvException(exception); mLog.AppendFormat("\"{0}\";", currError.ToJsonString().Replace("\"", "\"\"")); } else { mLog.Append("none;none;"); } var hex = exception as HttpException; var code = hex != null?hex.GetHttpCode() : 0; switch (code) { case 0: LogErrHttp.Error(mLog.ToString()); break; case 404: LogErr404.Error(mLog.ToString()); break; case 500: LogErr500.Error(mLog.ToString()); break; default: LogErrHttp.Error(mLog.ToString()); break; } // Send email if (EnableErrorMailNotification && code != 404 && sendEmail) { if (currError != null) { SendMailError(currError); } } }
private static void SendMailError(AdvException currError) { if (HttpContext.Current != null && HttpContext.Current.Request.Url.AbsoluteUri.Contains("localhost")) { return; } lock (Sync) { var data = LogTempDataService.GetLogTempData(); if (data.MailErrorLastSend.DayOfYear < DateTime.Now.DayOfYear) { data.MailErrorCurrentCount = 0; } if (data.MailErrorCurrentCount < MaxMailsSendPerDay) { if (File.Exists(EmailNotificationTemplate)) { var template = new StringBuilder(File.ReadAllText(EmailNotificationTemplate)); template.Replace("CommonInfo_Header", Resource.Admin_MasterPageAdmin_BugTracker_CommonInfo); var outHtml = new StringBuilder(); outHtml.Append("<div class=\'tab-content\'>"); outHtml.Append("<table>"); outHtml.Append("<tr><td colspan='2'><hr/></td></td></tr>"); outHtml.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", "Message", currError.ExceptionData.ManualMessage.IsNullOrEmpty() ? "none" : currError.ExceptionData.ManualMessage); outHtml.Append("<tr><td colspan='2'><hr/></td></td></tr>"); outHtml.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", "ExceptionMessage", currError.ExceptionData.ExceptionMessage); outHtml.Append("<tr><td colspan='2'><hr/></td></td></tr>"); outHtml.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", "ExceptionStackTrace", currError.ExceptionData.ExceptionStackTrace.IsNullOrEmpty() ? "none" : currError.ExceptionData.ExceptionStackTrace); outHtml.Append("<tr><td colspan='2'><hr/></td></td></tr>"); outHtml.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", "InnerExceptionMessage", currError.ExceptionData.InnerExceptionMessage.IsNullOrEmpty() ? "none" : currError.ExceptionData.InnerExceptionMessage); outHtml.Append("<tr><td colspan='2'><hr/></td></td></tr>"); outHtml.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", "InnerExceptionStackTrace", currError.ExceptionData.InnerExceptionStackTrace.IsNullOrEmpty() ? "none" : currError.ExceptionData.InnerExceptionStackTrace); outHtml.Append("<tr><td colspan='2'><hr/></td></td></tr>"); foreach (var key in currError.ExceptionData.Parameters.Keys) { outHtml.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", key, currError.ExceptionData.Parameters[key]); } outHtml.Append("<tr><td colspan='2'><hr/></td></td></tr>"); outHtml.Append("</table>"); outHtml.Append("</div>"); template.Replace("CommonInfo_Body", outHtml.ToString()); template.Replace("Request_Header", Resource.Admin_MasterPageAdmin_BugTracker_Request); outHtml = new StringBuilder(); outHtml.Append("<div class=\'tab-content\'>"); if (currError.RequestData != null) { outHtml.Append("<table>"); foreach (var key in currError.RequestData.ColectionData.Keys) { outHtml.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", key, currError.RequestData.ColectionData[key]); } outHtml.Append("</table>"); } outHtml.Append("</div>"); template.Replace("Request_Body", outHtml.ToString()); template.Replace("Browser_Header", Resource.Admin_MasterPageAdmin_BugTracker_Browser); outHtml = new StringBuilder(); outHtml.Append("<div class=\'tab-content\'>"); if (currError.BrowserData != null) { outHtml.Append("<table>"); foreach (var key in currError.BrowserData.ColectionData.Keys) { outHtml.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", key, currError.BrowserData.ColectionData[key]); } outHtml.Append("</table>"); } outHtml.Append("</div>"); template.Replace("Browser_Body", outHtml.ToString()); template.Replace("Session_Header", Resource.Admin_MasterPageAdmin_BugTracker_Session); outHtml = new StringBuilder(); outHtml.Append("<div class=\'tab-content\'>"); if (currError.SessionData != null) { outHtml.Append("<table>"); foreach (var key in currError.SessionData.ColectionData.Keys) { outHtml.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", key, currError.SessionData.ColectionData[key]); } outHtml.Append("</table>"); } outHtml.Append("</div>"); template.Replace("Session_Body", outHtml.ToString()); ILoggerRepository rep = LogManager.GetRepository(); foreach (IAppender appender in rep.GetAppenders()) { if (appender is SmtpAppender) { // Loop exception here. SettingsMain.SiteUrl - Лезет в базу. // ((SmtpAppender)appender).Subject = SettingsMain.SiteUrl + " " + SettingsGeneral.SiteVersion; ((SmtpAppender)appender).Subject = SettingsGeneral.AbsoluteUrl + " " + SettingsGeneral.SiteVersion; var emailLog1 = LogManager.GetLogger("EmailErr"); emailLog1.Error(template.ToString()); } } } data.MailErrorCurrentCount++; data.MailErrorLastSend = DateTime.Now; } LogTempDataService.UpdateLogTempData(data); } }