/// <summary> /// Job that updates the JobPulse setting with the current date/time. /// This will allow us to notify an admin if the jobs stop running. /// /// Called by the <see cref="IScheduler" /> when a /// <see cref="ITrigger" /> fires that is associated with /// the <see cref="IJob" />. /// </summary> public virtual void Execute(IJobExecutionContext context) { // get the job map JobDataMap dataMap = context.JobDetail.JobDataMap; // delete accounts that have not been confirmed in X hours int userExpireHours = Int32.Parse(dataMap.GetString("HoursKeepUnconfirmedAccounts")); DateTime userAccountExpireDate = DateTime.Now.Add(new TimeSpan(userExpireHours * -1, 0, 0)); UserService userService = new UserService(); foreach (var user in userService.Queryable().Where(u => u.IsConfirmed == false && u.CreationDate < userAccountExpireDate)) { userService.Delete(user, null); } userService.Save(null, null); // purge exception log int exceptionExpireDays = Int32.Parse(dataMap.GetString("DaysKeepExceptions")); DateTime exceptionExpireDate = DateTime.Now.Add(new TimeSpan(userExpireHours * -1, 0, 0)); ExceptionLogService exceptionLogService = new ExceptionLogService(); foreach (var exception in exceptionLogService.Queryable().Where(e => e.ExceptionDate < exceptionExpireDate)) { exceptionLogService.Delete(exception, null); } exceptionLogService.Save(null, null); }
private void LogError( Exception ex, int parentException, string status, System.Web.HttpContext context ) { try { // get the current user Rock.CMS.User user = Rock.CMS.UserService.GetCurrentUser(); // save the exception info to the db ExceptionLogService service = new ExceptionLogService(); ExceptionLog exceptionLog = new ExceptionLog(); ; exceptionLog.ParentId = parentException; exceptionLog.ExceptionDate = DateTime.Now; if ( ex.InnerException != null ) exceptionLog.HasInnerException = true; exceptionLog.Description = ex.Message; exceptionLog.StackTrace = ex.StackTrace; exceptionLog.Source = ex.Source; exceptionLog.StatusCode = status; if ( context.Items["Rock:SiteId"] != null ) exceptionLog.SiteId = Int32.Parse( context.Items["Rock:SiteId"].ToString() ); if ( context.Items["Rock:PageId"] != null ) exceptionLog.PageId = Int32.Parse( context.Items["Rock:PageId"].ToString() ); exceptionLog.ExceptionType = ex.GetType().Name; exceptionLog.PageUrl = context.Request.RawUrl; exceptionLog.QueryString = context.Request.QueryString.ToString(); // write cookies StringBuilder cookies = new StringBuilder(); cookies.Append("<table class=\"cookies\">"); foreach ( string cookie in context.Request.Cookies ) cookies.Append( "<tr><td><b>" + cookie + "</b></td><td>" + context.Request.Cookies[cookie].Value + "</td></tr>" ); cookies.Append( "</table>" ); exceptionLog.Cookies = cookies.ToString(); // write form items StringBuilder formItems = new StringBuilder(); cookies.Append( "<table class=\"formItems\">" ); foreach ( string formItem in context.Request.Form ) cookies.Append( "<tr><td><b>" + formItem + "</b></td><td>" + context.Request.Form[formItem].ToString() + "</td></tr>" ); cookies.Append( "</table>" ); exceptionLog.Form = formItems.ToString(); // write server vars StringBuilder serverVars = new StringBuilder(); cookies.Append( "<table class=\"server-variables\">" ); foreach ( string serverVar in context.Request.ServerVariables ) serverVars.Append( "<tr><td><b>" + serverVar + "</b></td><td>" + context.Request.ServerVariables[serverVar].ToString() + "</td></tr>" ); cookies.Append( "</table>" ); exceptionLog.ServerVariables = serverVars.ToString(); if (user != null) exceptionLog.PersonId = user.PersonId; service.Add( exceptionLog, null ); service.Save( exceptionLog, null ); // log inner exceptions if ( ex.InnerException != null ) LogError( ex.InnerException, exceptionLog.Id, status, context ); } catch ( Exception exception ) { // if you get an exception while logging an exception I guess you're hosed... } }
/// <summary> /// Job that updates the JobPulse setting with the current date/time. /// This will allow us to notify an admin if the jobs stop running. /// /// Called by the <see cref="IScheduler" /> when a /// <see cref="ITrigger" /> fires that is associated with /// the <see cref="IJob" />. /// </summary> public virtual void Execute(IJobExecutionContext context) { // get the job map JobDataMap dataMap = context.JobDetail.JobDataMap; // delete accounts that have not been confirmed in X hours int userExpireHours = Int32.Parse( dataMap.GetString( "HoursKeepUnconfirmedAccounts" ) ); DateTime userAccountExpireDate = DateTime.Now.Add( new TimeSpan( userExpireHours * -1,0,0 ) ); UserService userService = new UserService(); foreach (var user in userService.Queryable().Where(u => u.IsConfirmed == false && u.CreationDate < userAccountExpireDate)) { userService.Delete( user, null ); } userService.Save( null, null ); // purge exception log int exceptionExpireDays = Int32.Parse( dataMap.GetString( "DaysKeepExceptions" ) ); DateTime exceptionExpireDate = DateTime.Now.Add( new TimeSpan( userExpireHours * -1, 0, 0 ) ); ExceptionLogService exceptionLogService = new ExceptionLogService(); foreach ( var exception in exceptionLogService.Queryable().Where( e => e.ExceptionDate < exceptionExpireDate ) ) { exceptionLogService.Delete( exception, null ); } exceptionLogService.Save( null, null ); }
/// <summary> /// Logs the error to database /// </summary> /// <param name="ex">The ex.</param> /// <param name="parentException">The parent exception.</param> /// <param name="status">The status.</param> /// <param name="context">The context.</param> private void LogError(Exception ex, int parentException, string status, System.Web.HttpContext context) { try { // get the current user Rock.Model.UserLogin userLogin = Rock.Model.UserLoginService.GetCurrentUser(); // save the exception info to the db ExceptionLogService service = new ExceptionLogService(); ExceptionLog exceptionLog = new ExceptionLog();; exceptionLog.ParentId = parentException; exceptionLog.ExceptionDateTime = DateTime.Now; if (ex.InnerException != null) { exceptionLog.HasInnerException = true; } exceptionLog.Description = ex.Message; exceptionLog.StackTrace = ex.StackTrace; exceptionLog.Source = ex.Source; exceptionLog.StatusCode = status; if (context.Items["Rock:SiteId"] != null) { exceptionLog.SiteId = Int32.Parse(context.Items["Rock:SiteId"].ToString()); } if (context.Items["Rock:PageId"] != null) { exceptionLog.PageId = Int32.Parse(context.Items["Rock:PageId"].ToString()); } exceptionLog.ExceptionType = ex.GetType().Name; exceptionLog.PageUrl = context.Request.RawUrl; exceptionLog.QueryString = context.Request.QueryString.ToString(); // write cookies StringBuilder cookies = new StringBuilder(); cookies.Append("<table class=\"cookies\">"); foreach (string cookie in context.Request.Cookies) { cookies.Append("<tr><td><b>" + cookie + "</b></td><td>" + context.Request.Cookies[cookie].Value + "</td></tr>"); } cookies.Append("</table>"); exceptionLog.Cookies = cookies.ToString(); // write form items StringBuilder formItems = new StringBuilder(); cookies.Append("<table class=\"formItems\">"); foreach (string formItem in context.Request.Form) { cookies.Append("<tr><td><b>" + formItem + "</b></td><td>" + context.Request.Form[formItem].ToString() + "</td></tr>"); } cookies.Append("</table>"); exceptionLog.Form = formItems.ToString(); // write server vars StringBuilder serverVars = new StringBuilder(); serverVars.Append("<table class=\"server-variables\">"); foreach (string serverVar in context.Request.ServerVariables) { serverVars.Append("<tr><td><b>" + serverVar + "</b></td><td>" + context.Request.ServerVariables[serverVar].ToString() + "</td></tr>"); } serverVars.Append("</table>"); exceptionLog.ServerVariables = serverVars.ToString(); if (userLogin != null) { exceptionLog.CreatedByPersonId = userLogin.PersonId; } service.Add(exceptionLog, null); service.Save(exceptionLog, null); // log inner exceptions if (ex.InnerException != null) { LogError(ex.InnerException, exceptionLog.Id, status, context); } } catch (Exception) { // If logging the exception fails, write the exception to a file try { string directory = AppDomain.CurrentDomain.BaseDirectory; directory = Path.Combine(directory, "Logs"); // check that directory exists if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } // create full path to the fie string filePath = Path.Combine(directory, "RockExceptions.csv"); // write to the file System.IO.File.AppendAllText(filePath, string.Format("{0},{1},\"{2}\"\r\n", DateTime.Now.ToString(), EventLogEntryType.Error, ex.Message)); } catch { // failed to write to database and also failed to write to log file, so there is nowhere to log this error } } }
private void LogError(Exception ex, int parentException, string status, System.Web.HttpContext context) { try { // get the current user Rock.CMS.User user = Rock.CMS.UserService.GetCurrentUser(); // save the exception info to the db ExceptionLogService service = new ExceptionLogService(); ExceptionLog exceptionLog = new ExceptionLog();; exceptionLog.ParentId = parentException; exceptionLog.ExceptionDate = DateTime.Now; if (ex.InnerException != null) { exceptionLog.HasInnerException = true; } exceptionLog.Description = ex.Message; exceptionLog.StackTrace = ex.StackTrace; exceptionLog.Source = ex.Source; exceptionLog.StatusCode = status; if (context.Items["Rock:SiteId"] != null) { exceptionLog.SiteId = Int32.Parse(context.Items["Rock:SiteId"].ToString()); } if (context.Items["Rock:PageId"] != null) { exceptionLog.PageId = Int32.Parse(context.Items["Rock:PageId"].ToString()); } exceptionLog.ExceptionType = ex.GetType().Name; exceptionLog.PageUrl = context.Request.RawUrl; exceptionLog.QueryString = context.Request.QueryString.ToString(); // write cookies StringBuilder cookies = new StringBuilder(); cookies.Append("<table class=\"cookies\">"); foreach (string cookie in context.Request.Cookies) { cookies.Append("<tr><td><b>" + cookie + "</b></td><td>" + context.Request.Cookies[cookie].Value + "</td></tr>"); } cookies.Append("</table>"); exceptionLog.Cookies = cookies.ToString(); // write form items StringBuilder formItems = new StringBuilder(); cookies.Append("<table class=\"formItems\">"); foreach (string formItem in context.Request.Form) { cookies.Append("<tr><td><b>" + formItem + "</b></td><td>" + context.Request.Form[formItem].ToString() + "</td></tr>"); } cookies.Append("</table>"); exceptionLog.Form = formItems.ToString(); // write server vars StringBuilder serverVars = new StringBuilder(); cookies.Append("<table class=\"server-variables\">"); foreach (string serverVar in context.Request.ServerVariables) { serverVars.Append("<tr><td><b>" + serverVar + "</b></td><td>" + context.Request.ServerVariables[serverVar].ToString() + "</td></tr>"); } cookies.Append("</table>"); exceptionLog.ServerVariables = serverVars.ToString(); if (user != null) { exceptionLog.PersonId = user.PersonId; } service.Add(exceptionLog, null); service.Save(exceptionLog, null); // log inner exceptions if (ex.InnerException != null) { LogError(ex.InnerException, exceptionLog.Id, status, context); } } catch (Exception exception) { // if you get an exception while logging an exception I guess you're hosed... } }
/// <summary> /// Job that executes routine Rock cleanup tasks /// /// Called by the <see cref="IScheduler" /> when a /// <see cref="ITrigger" /> fires that is associated with /// the <see cref="IJob" />. /// </summary> public virtual void Execute(IJobExecutionContext context) { // get the job map JobDataMap dataMap = context.JobDetail.JobDataMap; // delete accounts that have not been confirmed in X hours int userExpireHours = Int32.Parse(dataMap.GetString("HoursKeepUnconfirmedAccounts")); DateTime userAccountExpireDate = DateTime.Now.Add(new TimeSpan(userExpireHours * -1, 0, 0)); var userLoginService = new UserLoginService(); foreach (var user in userLoginService.Queryable().Where(u => u.IsConfirmed == false && u.CreationDateTime < userAccountExpireDate).ToList()) { userLoginService.Delete(user, null); userLoginService.Save(user, null); } // purge exception log int exceptionExpireDays = Int32.Parse(dataMap.GetString("DaysKeepExceptions")); DateTime exceptionExpireDate = DateTime.Now.Add(new TimeSpan(exceptionExpireDays * -1, 0, 0, 0)); ExceptionLogService exceptionLogService = new ExceptionLogService(); foreach (var exception in exceptionLogService.Queryable().Where(e => e.ExceptionDateTime < exceptionExpireDate).ToList()) { exceptionLogService.Delete(exception, null); exceptionLogService.Save(exception, null); } // purge audit log int auditExpireDays = Int32.Parse(dataMap.GetString("AuditLogExpirationDays")); DateTime auditExpireDate = DateTime.Now.Add(new TimeSpan(auditExpireDays * -1, 0, 0, 0)); AuditService auditService = new AuditService(); foreach (var audit in auditService.Queryable().Where(a => a.DateTime < auditExpireDate).ToList()) { auditService.Delete(audit, null); auditService.Save(audit, null); } // clean the cached file directory //get the attributes string cacheDirectoryPath = dataMap.GetString("BaseCacheDirectory"); int cacheExpirationDays = int.Parse(dataMap.GetString("DaysKeepCachedFiles")); DateTime cacheExpirationDate = DateTime.Now.Add(new TimeSpan(cacheExpirationDays * -1, 0, 0, 0)); //if job is being run by the IIS scheduler and path is not null if (context.Scheduler.SchedulerName == "RockSchedulerIIS" && !String.IsNullOrEmpty(cacheDirectoryPath)) { //get the physical path of the cache directory cacheDirectoryPath = System.Web.Hosting.HostingEnvironment.MapPath(cacheDirectoryPath); } //if directory is not blank and cache expiration date not in the future if (!String.IsNullOrEmpty(cacheDirectoryPath) && cacheExpirationDate <= DateTime.Now) { //Clean cache directory CleanCacheDirectory(cacheDirectoryPath, cacheExpirationDate); } // clean out any temporary binary files BinaryFileService binaryFileService = new BinaryFileService(); foreach (var binaryFile in binaryFileService.Queryable().Where(bf => bf.IsTemporary == true).ToList()) { if (binaryFile.LastModifiedDateTime < DateTime.Now.AddDays(-1)) { binaryFileService.Delete(binaryFile, null); binaryFileService.Save(binaryFile, null); } } }