예제 #1
0
        /// <summary>
        /// use this method if userName is known
        /// Log exception and send email notification if it is set on in Web.config
        /// </summary>
        /// <param name="exception"></param>
        /// <remarks></remarks>
        public static void LogException(ref System.Exception exception, string userName = "")
        {
            try {
                //if (SharedSettings.Settings == null) {
                //    throw new Exception("Shared Settings is empty! this means settings.xml needs to be loaded in the application");
                //}
                //      dynamic exceptionHand = SharedSettings.Settings;

                Guid?userID = AppSettings.GetUserIDByUserName(userName);

                if (LogExceptionOrNot(System.DateTime.Now))
                {
                    StringBuilder errorMsg   = new StringBuilder(ExceptionHandling.BuildErrorMessage(ref exception, userName));
                    StackTrace    stacktrace = new StackTrace();
                    dynamic       x          = ((System.Reflection.MethodBase)stacktrace.GetFrame(1).GetMethod()).ReflectedType;
                    var           _with1     = errorMsg;
                    _with1.Append("\n" + x.ToString() + "\n");
                    _with1.Append(stacktrace.GetFrame(1).ToString() + "\n");
                    if (exception.InnerException != null)
                    {
                        _with1.Append(exception.InnerException.ToString() + "\n");
                    }
                    _with1.Append(exception.Source + "\n");
                    _with1.Append(exception.TargetSite + "\n");
                    if (exception.InnerException != null)
                    {
                        System.Diagnostics.Debug.Print("<h2> INNER EXC </h2> \t" + exception.InnerException.ToString());
                    }
                    System.Diagnostics.Debug.Print("<h2> MESSAGE </h2> \t" + exception.Message);
                    System.Diagnostics.Debug.Print("<h2> STACK-TRACE </h2> \t" + exception.StackTrace);
                    System.Diagnostics.Debug.Print("<h2> SOURCE </h2> \t" + exception.Source);
                    System.Diagnostics.Debug.Print("<h2> TARGET </h2> \t" + exception.TargetSite);

                    //log exception to text file too
                    //Call LogToTextfile(errorMsg)

                    //log exception to database
                    LogExceptionToDatabase(exception, ExceptionHandling.RecordToDataBase, userID);

                    //send email
                    //           SendEmailException(ref exception);
                }
                else
                {
                    System.Diagnostics.Debug.Print("Not sending exception as duplicate!" + exception.ToString());
                }
            }
            catch (Exception ex2) {
                //do nothing if there is an exception logging an exception - really bad!
                System.Diagnostics.Debug.Print("Error in LogException" + "\n" + ex2.InnerException + "\n" + ex2.Message + "\n" + ex2.StackTrace);
            }
        }