Ejemplo n.º 1
0
        /// <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
                }
            }
        }
Ejemplo n.º 2
0
        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...
            }
        }
Ejemplo n.º 3
0
        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...
            }
        }