aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Updater/App.xaml
blob: c3e63550a71db880a65fcacbe06bb92e82d457f6 (plain)
1
2
3
4
5
6
7
8
9
<Application x:Class="Tango.MachineStudio.Updater.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Tango.MachineStudio.Updater"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>
ic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Tango.Logging;

namespace Tango.Logging
{
    /// <summary>
    /// Represents an optimized WPF global exception trapper.
    /// </summary>
    /// <seealso cref="Tango.Logging.IGlobalExceptionTrapper" />
    public class WpfGlobalExceptionTrapper : IGlobalExceptionTrapper
    {
        private DateTime _lastGlobalExceptionTime = DateTime.Now.AddMinutes(-1);
        private Application _app;

        /// <summary>
        /// Occurs when the global exception trapper has detected an unhandled exception.
        /// </summary>
        public event EventHandler<ApplicationCrashedEventArgs> ApplicationCrashed;

        /// <summary>
        /// Initializes the specified application.
        /// </summary>
        /// <param name="app">The application.</param>
        public void Initialize(Application app)
        {
            _app = app;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            app.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
            Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
        }

        /// <summary>
        /// Use only when need to simulate application crash!
        /// </summary>
        public void Disable()
        {
            AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
            _app.Dispatcher.UnhandledException -= Dispatcher_UnhandledException;
            Application.Current.DispatcherUnhandledException -= Current_DispatcherUnhandledException;
            TaskScheduler.UnobservedTaskException -= TaskScheduler_UnobservedTaskException;
        }

        /// <summary>
        /// Handles the UnobservedTaskException event of the TaskScheduler control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="UnobservedTaskExceptionEventArgs"/> instance containing the event data.</param>
        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            e.SetObserved();
            OnApplicationCrash(e.Exception);
        }

        /// <summary>
        /// Handles the DispatcherUnhandledException event of the Current control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param>
        private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = OnApplicationCrash(e.Exception);
        }

        /// <summary>
        /// Handles the UnhandledException event of the Dispatcher control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param>
        private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = OnApplicationCrash(e.Exception);
        }

        /// <summary>
        /// Handles the UnhandledException event of the CurrentDomain control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            OnApplicationCrash(new Exception(e.ExceptionObject.ToStringSafe()));
        }

        /// <summary>
        /// Called when any unhandled application exception has occurred and write to log.
        /// </summary>
        /// <param name="error">The error.</param>
        private bool OnApplicationCrash(Exception exception)
        {
            if (DateTime.Now < _lastGlobalExceptionTime.AddSeconds(1))
            {
                _lastGlobalExceptionTime = DateTime.Now;
                return true;
            }

            _lastGlobalExceptionTime = DateTime.Now;

            ApplicationCrashedEventArgs e = new ApplicationCrashedEventArgs(exception);

            ApplicationCrashed?.Invoke(this, e);

            return e.TryRecover;
        }

        public Task<MessageLogItem> GetLastApplicationCrashEventLog(int maxMinutes = 10)
        {
            return Task.Factory.StartNew<MessageLogItem>(() =>
            {
                MessageLogItem logItem = null;

                try
                {
                    var applicationEvents = new EventLog("Application");
                    var events = applicationEvents.Entries.Cast<EventLogEntry>().Where(x => x.EntryType == EventLogEntryType.Error && x.Source == ".NET Runtime" && x.TimeWritten > DateTime.Now.AddMinutes(-maxMinutes)).OrderByDescending(x => x.TimeWritten).ToList();

                    Regex reg = new Regex("Application: (.+)");

                    foreach (var ev in events)
                    {
                        Match match = reg.Match(ev.Message);
                        if (match.Groups.Count > 1)
                        {
                            String exeName = match.Groups[1].Value;
                            String assemblyName = Path.GetFileNameWithoutExtension(exeName);

                            if (assemblyName == Assembly.GetEntryAssembly().GetName().Name)
                            {
                                logItem = new MessageLogItem();
                                logItem.Message = "Application terminated unexpectedly in the previous run!\n";
                                logItem.Message += "This crash report was retrieved from the windows event logs.\n";
                                logItem.Message += "---------------------------------------------------------------------------\n\n";
                                logItem.Message += ev.Message;
                                logItem.TimeStamp = DateTime.Now;
                                logItem.Category = LogCategory.Critical;
                                break;
                            }
                        }
                    }
                }
                catch { }

                return logItem;
            });
        }
    }
}