Exemplo n.º 1
0
        public static void LogToFileAsXML(TFSExceptionReport ex, DateTime now)
        {
            try
            {
                //Get exception logger
                var log = LogManager.GetLogger(repo.Name, XMLReporterLogger);
                ex.ExceptionEntity.Comment =
                    $"{now.ToString("G", CultureInfo.InvariantCulture)}: {ex.ExceptionEntity.Comment}";
                var          ser    = new XmlSerializer(typeof(ExceptionEntity));
                var          outStr = new StringBuilder();
                StringWriter mem    = null;
                try
                {
                    mem = new StringWriter(outStr);
                    using (var writer = new XmlTextWriter(mem))
                    {
                        mem = null;
                        writer.Formatting = Formatting.Indented;
                        ser.Serialize(writer, ex.ExceptionEntity);
                    }
                }
                finally
                {
                    mem?.Dispose();
                }

                log.Info(outStr.ToString());
                StoreAsFile(outStr.ToString(), "xml", ex.ExceptionEntity.StackTrace, now);
            }
            catch
            {
                //No more falback solutions.
                //need to catch to avoid circular exception
            }
        }
Exemplo n.º 2
0
        public static void LogToFile(TFSExceptionReport ex)
        {
            var now = DateTime.Now;

            try
            {
                //Get exception logger
                var _log = LogManager.GetLogger(repo.Name, ReporterLogger);


                //get exceptionentity
                // ReSharper disable PossibleNullReferenceException
                object exEnt = ex.ExceptionEntity;

                // ReSharper restore PossibleNullReferenceException

                if (_log != null && Settings.Default.LogExceptionReports)
                {
                    var txt = exEnt.GetType().GetProperties().Select(
                        (prop) =>
                        prop.Name + " = " +
                        (prop.GetValue(exEnt, BindingFlags.Default, null, null, null) ?? "NO VALUE").ToString())
                              .Aggregate((x, y) => x + System.Environment.NewLine + y);
                    _log.Info(txt);
                    StoreAsFile(txt, "log", ex.ExceptionEntity.StackTrace, now);
                }
            }
            catch
            {
                //No more falback solutions.
                //need to catch to avoid circular exception
            }
            LogToFileAsXML(ex, now);
        }
Exemplo n.º 3
0
        private static void LogToFile(System.Exception e)
        {
            var report = new TFSExceptionReport(ApplicationName, Reporter, Reporter, e, Version,
                                                "Exception reported w/o description");

            ReportLogger.LogToFile(report);
        }
Exemplo n.º 4
0
        private static void DoPost(string description)
        {
            try
            {
                //create exception entity
                var report = new TFSExceptionReport
                             (
                    ApplicationName,
                    Reporter,
                    Reporter, TheException,
                    Version,
                    description);

                //log to file
                ReportLogger.LogToFile(report);

                //post to service.
                var result = (!DoNotSend) ? report.Post() : null;

                ReportLogger.LogInfo("Result posted");
                //if error show to user.
                if (result != null)
                {
                    ReportLogger.LogExceptionsDuringDelivery(
                        new FileLoadException(
                            "Failed to deliver exception to url = '" + ServiceSettings.ServiceUrl + "'", result));
                    try
                    {
                        //failed to deliver exception display for user.
                        _form.ShowDeliveryFailure(result.Message, result);
                    }
                    catch (System.Exception ex)
                    {
                        //failed to show delivery failure... just log
                        ReportLogger.LogExceptionsDuringDelivery(
                            new InvalidOperationException("Failed to show delivery exception", ex));
                    }
                }
            }
            catch (System.Exception ex)
            {
                ReportLogger.LogExceptionsDuringDelivery(
                    new FileLoadException("Exception during TFS exception report create or post", ex));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// report W/O GUI.
        /// </summary>
        /// <param name="currentNtUser"></param>
        /// <param name="version"></param>
        /// <param name="applicationName"></param>
        /// <param name="e"></param>
        internal static void ReportExceptionWithNoGUI(string currentNtUser, string version, string applicationName, System.Exception e)
        {
            try
            {
                //ignore result since we are not using any UI to provide user feedback.
                //any errors will be logged by the Post function.
                var report =
                    new TFSExceptionReport(
                        applicationName,
                        currentNtUser,
                        currentNtUser, e,
                        version,
                        "Exception reported w/o description");

                ReportLogger.LogToFile(report);

                report.Post();
            }
            catch (System.Exception ex)
            {
                ReportLogger.LogExceptionsDuringDelivery(new System.Exception("Failed to deliver exception (no GUI)", ex));
            }
        }
Exemplo n.º 6
0
        internal static bool OnException(System.Exception e, bool isTerminating)
        {
            ExceptionReporting?.Invoke(e, EventArgs.Empty);
            var  proc    = Process.GetCurrentProcess();
            bool fromSTA = true;

            //avoid recursive reporting when IsTerminating is true, since we have registered both Main Form and AppDomain with unhandled exceptions
            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
            {
                if (_previousException != null && e != null && _previousException.ToString() == e.ToString())
                {
                    ReportLogger.LogInfo(new ArgumentException("Trying to report on the same exception.", e).ToString());
                    //same as previous
                    return(_tryContinueAfterException);
                }

                _previousException = e;
            }

            ReportLogger.LogInfo("Received exception  (isTerminating = " + isTerminating + ")");

            TheException = e;

            try
            {
                //use ExceptionRegistrator.UseReportGUI to control if UI is to be used or not.
                if (!useReportGUI)
                {
                    ReportExceptionWithNoGUI(Reporter, Version, ApplicationName, e);
                    return(false);
                }
                // XAML issue with MTA threads.
                if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
                {
                    LogToFile(e);
                    ReportInSTA(e, isTerminating);
                    fromSTA = false;
                    return(_tryContinueAfterException);
                }

                //only report one exception at the time.
                lock (_syncObject)
                {
                    string errorText = CreateExceptionText(e);

                    //show the error to the user and collect a description of the error from the user.
                    try
                    {
                        //show the exception using the registered IReportForm.
                        //if return value is false -> cancel
                        if (!_form.ShowException(errorText,
                                                 //this callback is to be used when the form click on the Send button.
                                                 DoPost))
                        {
                            //cancel report exception to log
                            LogToFile(e);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        //log report exception
                        var report = new TFSExceptionReport(ApplicationName, Reporter, Reporter, e, Version,
                                                            "THIS IS A AUTO GENERATED TEXT: Failed to show exception report.");

                        ReportLogger.LogToFile(report);

                        ReportLogger.LogExceptionsDuringDelivery(
                            new InvalidOperationException("Failed to show exception report.", ex));
                    }
                }
            }
            catch (ThreadAbortException terminate)
            {
                try
                {
                    _form.Window.Close();
                }
                catch
                {
                    //ignore...
                }
                ReportLogger.LogInfo(new ThreadStateException("Report form is terminating.", terminate).ToString());
            }
            finally
            {
                //we should inform the user that the application is about to terminate.
                // currently only support for WPF.
                if (_showExitAppWindow && isTerminating && fromSTA && Application.Current != null)
                {
                    try
                    {
                        var terminateWindow = new IsTerminatingWindow();
                        terminateWindow.Topmost = true;
                        terminateWindow.Show();

                        //sleep for 5000 seconds.
                        Thread.Sleep(5000);

                        terminateWindow.Close();
                    }
                    catch
                    {
                        //for now ignore ... this will happen if thread is MTA...but no more time to code.
                    }
                }
            }

            return(_tryContinueAfterException);
        }