-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathApp.xaml.cs
178 lines (157 loc) · 6.52 KB
/
App.xaml.cs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Threading;
using CameraControl.Devices;
using CameraControl.Devices.Classes;
using Capture.Workflow.Classes;
using Capture.Workflow.Core;
using Capture.Workflow.Core.Classes;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Layout;
namespace Capture.Workflow
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static readonly ILog _log = LogManager.GetLogger("Capture.Workflow");
private void Application_Startup(object sender, StartupEventArgs e)
{
Current.DispatcherUnhandledException += AppDispatcherUnhandledException;
string procName = Process.GetCurrentProcess().ProcessName;
// get the list of all processes by that name
Process[] processes = Process.GetProcessesByName(procName);
if (processes.Length > 1)
{
MessageBox.Show("Application already running !");
Shutdown(-1);
return;
}
Configure(Path.Combine(Settings.Instance.LogFolder, "Capture.Workflow.log"));
GoogleAnalytics.Instance.TrackEvent("Application", "Start");
WorkflowManager.Instance.LoadPlugins("Capture.Workflow.Plugins.dll");
}
private void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
//#if DEBUG
// // In debug mode do not custom-handle the exception, let Visual Studio handle it
// e.Handled = false;
//#else
// ShowUnhandeledException(e);
//#endif
ShowUnhandeledException(e);
}
private void ShowUnhandeledException(DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = true;
Log.Error("Unhandled error ", e.Exception);
string errorMessage =
string.Format("Unhanded Exception {0}", e.Exception.Message + (e.Exception.InnerException != null
? "\n" +
e.Exception.InnerException.Message
: null));
GoogleAnalytics.Instance.TrackException(e.Exception.Message, true);
if (e.Exception.GetType() == typeof(MissingMethodException))
{
Log.Error("Damaged installation. Application exiting ");
MessageBox.Show(
"Application crash !! Damaged installation!\nPlease unintall the aplication from control panel and reinstall it!");
}
else
{
SendCrashReport(e.Exception);
MessageBox.Show(errorMessage, "Application crash !!", MessageBoxButton.OK, MessageBoxImage.Error);
}
Current?.Shutdown();
}
private void SendCrashReport(Exception e)
{
try
{
var body = "Version :" + Assembly.GetExecutingAssembly().GetName().Version + "\n" +
"Client Id" + (Settings.Instance.ClientId ?? "") + "\n" ;
var error = "";
if (e != null)
{
error = e.Message;
body += e.StackTrace+"\n";
if (e.InnerException != null)
{
error = e.InnerException.Message;
body += "----------------------------------" + "\n";
body += e.InnerException.StackTrace;
}
}
Utils.SendEmail(body, "Capture.Workflow Crash report - "+error,"[email protected]" , "[email protected]",Path.Combine(Settings.Instance.LogFolder, "Capture.Workflow.log") );
}
catch (Exception )
{
}
}
public static void Configure(string logFile)
{
Utils.CreateFolder(logFile);
Configure("Capture.Workflow", logFile);
Log.LogDebug += Log_LogDebug;
Log.LogError += Log_LogError;
Log.Debug("------------------------------===========================Application starting===========================------------------------------");
try
{
Log.Debug("Application version : " + Assembly.GetEntryAssembly().GetName().Version);
ServiceProvider.Instance.DeviceManager.AddFakeCamera();
ServiceProvider.Instance.DeviceManager.ConnectToCamera();
}
catch { }
}
private static void Log_LogError(LogEventArgs e)
{
_log.Error(e.Message, e.Exception);
}
private static void Log_LogDebug(LogEventArgs e)
{
_log.Debug(e.Message, e.Exception);
}
public static void Configure(string appfolder, string logFile)
{
bool isConfigured = _log.Logger.Repository.Configured;
if (!isConfigured)
{
// Setup RollingFileAppender
var fileAppender = new RollingFileAppender
{
Layout =
new PatternLayout(
"%d [%t]%-5p %c [%x] - %m%n"),
MaximumFileSize = "10000KB",
MaxSizeRollBackups = 5,
RollingStyle = RollingFileAppender.RollingMode.Size,
AppendToFile = true,
File = logFile,
ImmediateFlush = true,
LockingModel = new FileAppender.MinimalLock(),
Name = "XXXRollingFileAppender"
};
fileAppender.ActivateOptions(); // IMPORTANT, creates the file
BasicConfigurator.Configure(fileAppender);
#if DEBUG
// Setup TraceAppender
TraceAppender ta = new TraceAppender();
ta.Layout = new PatternLayout("%d [%t]%-5p %c [%x] - %m%n");
BasicConfigurator.Configure(ta);
#endif
}
}
private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
QueueManager.Instance.Stop();
Settings.Instance.Save();
GoogleAnalytics.Instance.TrackEvent("Application","Stop");
}
}
}