blob: 3409ffdc4421364a1137ac30f69a555a3286299c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
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;
});
}
}
}
|