diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs | 65 |
1 files changed, 7 insertions, 58 deletions
diff --git a/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs b/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs index 3409ffdc4..5ee714cb6 100644 --- a/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs +++ b/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs @@ -1,12 +1,7 @@ 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; @@ -21,7 +16,6 @@ namespace Tango.Logging 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. @@ -34,7 +28,6 @@ namespace Tango.Logging /// <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; @@ -42,17 +35,6 @@ namespace Tango.Logging } /// <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> @@ -101,58 +83,25 @@ namespace Tango.Logging { if (DateTime.Now < _lastGlobalExceptionTime.AddSeconds(1)) { + LogManager.Default.Log(exception, LogCategory.Critical); _lastGlobalExceptionTime = DateTime.Now; return true; } _lastGlobalExceptionTime = DateTime.Now; + LogManager.Default.Log("Application Crashed", LogCategory.Critical); + LogManager.Default.Log(exception, LogCategory.Critical); ApplicationCrashedEventArgs e = new ApplicationCrashedEventArgs(exception); ApplicationCrashed?.Invoke(this, e); - return e.TryRecover; - } - - public Task<MessageLogItem> GetLastApplicationCrashEventLog(int maxMinutes = 10) - { - return Task.Factory.StartNew<MessageLogItem>(() => + if (e.TryRecover) { - 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 { } + LogManager.Default.Log("Trying application recovery. Ignoring exception..."); + } - return logItem; - }); + return e.TryRecover; } } } |
