root / trunk / Pithos.Client.WPF / App.xaml.cs @ adfa4645
History | View | Annotate | Download (10.7 kB)
1 |
#region |
---|---|
2 |
/* ----------------------------------------------------------------------- |
3 |
* <copyright file="App.xaml.cs" company="GRNet"> |
4 |
* |
5 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or |
8 |
* without modification, are permitted provided that the following |
9 |
* conditions are met: |
10 |
* |
11 |
* 1. Redistributions of source code must retain the above |
12 |
* copyright notice, this list of conditions and the following |
13 |
* disclaimer. |
14 |
* |
15 |
* 2. Redistributions in binary form must reproduce the above |
16 |
* copyright notice, this list of conditions and the following |
17 |
* disclaimer in the documentation and/or other materials |
18 |
* provided with the distribution. |
19 |
* |
20 |
* |
21 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 |
* POSSIBILITY OF SUCH DAMAGE. |
33 |
* |
34 |
* The views and conclusions contained in the software and |
35 |
* documentation are those of the authors and should not be |
36 |
* interpreted as representing official policies, either expressed |
37 |
* or implied, of GRNET S.A. |
38 |
* </copyright> |
39 |
* ----------------------------------------------------------------------- |
40 |
*/ |
41 |
#endregion |
42 |
using System; |
43 |
using System.Collections.Generic; |
44 |
using System.IO; |
45 |
using System.Linq; |
46 |
using System.Reflection; |
47 |
using System.Text; |
48 |
using System.Threading; |
49 |
using System.Threading.Tasks; |
50 |
using System.Windows; |
51 |
using Caliburn.Micro; |
52 |
using Pithos.Client.WPF.Configuration; |
53 |
using Pithos.Client.WPF.Properties; |
54 |
using log4net.Appender; |
55 |
using log4net.Core; |
56 |
using log4net.Repository.Hierarchy; |
57 |
|
58 |
|
59 |
namespace Pithos.Client.WPF |
60 |
{ |
61 |
/// <summary> |
62 |
/// Interaction logic for App.xaml |
63 |
/// </summary> |
64 |
public partial class App : Application |
65 |
{ |
66 |
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType ); |
67 |
|
68 |
|
69 |
public App() |
70 |
{ |
71 |
|
72 |
//var instanceMutex=new Mutex() |
73 |
|
74 |
InitializeLogging(); |
75 |
|
76 |
|
77 |
DispatcherUnhandledException += OnDispatcherUnhandledException; |
78 |
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; |
79 |
TaskScheduler.UnobservedTaskException += OnUnobservedException; |
80 |
|
81 |
|
82 |
//Fix incorrect proxy settings by switching to default proxy, if the proxy server settings is empty |
83 |
if (Settings.Default.UseManualProxy && String.IsNullOrWhiteSpace(Settings.Default.ProxyServer)) |
84 |
{ |
85 |
Settings.Default.UseManualProxy = false; |
86 |
Settings.Default.UseDefaultProxy = true; |
87 |
} |
88 |
|
89 |
|
90 |
InitializeComponent(); |
91 |
} |
92 |
|
93 |
|
94 |
/* |
95 |
private void OnUpdateDetected(object sender, UpdateDetectedEventArgs e) |
96 |
{ |
97 |
Log.InfoFormat("Update Detected {0}",e.LatestVersion.Version); |
98 |
} |
99 |
*/ |
100 |
|
101 |
private static void InitializeLogging() |
102 |
{ |
103 |
log4net.Config.XmlConfigurator.Configure(); |
104 |
|
105 |
try |
106 |
{ |
107 |
var pithosDataPath = PithosSettings.PithosDataPath; |
108 |
if (!Directory.Exists(pithosDataPath)) |
109 |
Directory.CreateDirectory(pithosDataPath); |
110 |
|
111 |
var loggerRepository = (Hierarchy)log4net.LogManager.GetRepository(); |
112 |
|
113 |
var appenders = loggerRepository.GetAppenders(); |
114 |
|
115 |
var lossyAppender = appenders.OfType<BufferingForwardingAppender>() |
116 |
.FirstOrDefault(appender => appender.Name == "LossyFileAppender"); |
117 |
if (lossyAppender!=null) |
118 |
{ |
119 |
var dumpAppender = lossyAppender.Appenders.OfType<RollingFileAppender>().First(); |
120 |
dumpAppender.File = Path.Combine(pithosDataPath, "errorlog.xml"); |
121 |
dumpAppender.ActivateOptions(); |
122 |
} |
123 |
|
124 |
var debugAppender =appenders.OfType<RollingFileAppender>() |
125 |
.FirstOrDefault(a => a.Name == "DebugFileAppender"); |
126 |
if (debugAppender != null) |
127 |
{ |
128 |
debugAppender.File = Path.Combine(pithosDataPath, "debuglog.xml"); |
129 |
if (!Settings.Default.DebugLoggingEnabled) |
130 |
debugAppender.Threshold = Level.Off; |
131 |
debugAppender.ActivateOptions(); |
132 |
} |
133 |
} |
134 |
catch (Exception exc) |
135 |
{ |
136 |
Log.Error("Dumpl appender initialization failed",exc); |
137 |
} |
138 |
} |
139 |
|
140 |
private Mutex _singleInstanceMutex; |
141 |
|
142 |
protected override void OnStartup(StartupEventArgs e) |
143 |
{ |
144 |
if (!Settings.Default.StartOnSystemStartup && e.Args.Contains("startup")) |
145 |
{ |
146 |
Shutdown(); |
147 |
return; |
148 |
} |
149 |
|
150 |
bool firstInstance; |
151 |
_singleInstanceMutex = new Mutex(false, "PITHOSMUTEX", out firstInstance); |
152 |
if (!firstInstance) |
153 |
{ |
154 |
_singleInstanceMutex.Dispose(); |
155 |
_singleInstanceMutex = null; |
156 |
Shutdown(); |
157 |
return; |
158 |
} |
159 |
|
160 |
//Delay during startup |
161 |
if (e.Args.Contains("startup")) |
162 |
{ |
163 |
if (Settings.Default.StartupDelay>TimeSpan.Zero) |
164 |
Thread.Sleep(Settings.Default.StartupDelay); |
165 |
} |
166 |
|
167 |
var splashScreen = new SplashScreen("images/logo.png"); |
168 |
splashScreen.Show(true); |
169 |
|
170 |
base.OnStartup(e); |
171 |
} |
172 |
|
173 |
protected override void OnExit(ExitEventArgs e) |
174 |
{ |
175 |
try |
176 |
{ |
177 |
if (_singleInstanceMutex!=null) |
178 |
_singleInstanceMutex.Dispose(); |
179 |
_singleInstanceMutex = null; |
180 |
} |
181 |
catch { } |
182 |
base.OnExit(e); |
183 |
} |
184 |
|
185 |
private void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e) |
186 |
{ |
187 |
var messages=new List<UserMessage>(); |
188 |
e.Exception.Handle(exc=>{ |
189 |
messages.Add(new UserMessage |
190 |
{ |
191 |
Message = "Unexpected Exception", |
192 |
Details = exc.ToString(), |
193 |
Severity = Severity.Error |
194 |
}); |
195 |
return true; |
196 |
}); |
197 |
|
198 |
Log.Error("Unobserved Task Exception",e.Exception); |
199 |
|
200 |
var message = String.Format(@"{0}\r\n{1}\r\n\r\n{2}", |
201 |
WPF.Properties.Resources.Unexpected_Error, |
202 |
WPF.Properties.Resources.We_Apologize, |
203 |
WPF.Properties.Resources.Please_Submit_Error); |
204 |
ShowMessages("Oops!",message, messages); |
205 |
e.SetObserved(); |
206 |
} |
207 |
|
208 |
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) |
209 |
{ |
210 |
Log.Error("Unhandled exception", (Exception)e.ExceptionObject); |
211 |
|
212 |
var description = e.IsTerminating |
213 |
? WPF.Properties.Resources.Unexpected_Error_Terminating |
214 |
: WPF.Properties.Resources.Unexpected_Error; |
215 |
var message = String.Format(@"{0}\r\n{1}\r\n\r\n{2}", |
216 |
description, |
217 |
WPF.Properties.Resources.We_Apologize, |
218 |
WPF.Properties.Resources.Please_Submit_Error); |
219 |
|
220 |
|
221 |
var exc = ((Exception) e.ExceptionObject); |
222 |
var messages = new[]{ |
223 |
new UserMessage |
224 |
{ |
225 |
Message = exc.Message, |
226 |
Details = exc.ToString(), |
227 |
Severity = Severity.Error |
228 |
} |
229 |
}; |
230 |
|
231 |
|
232 |
ShowMessages("Oops!", message,messages); |
233 |
} |
234 |
|
235 |
void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) |
236 |
{ |
237 |
Log.Error("Unhandled Dispatcher exception", e.Exception); |
238 |
|
239 |
var messages = new[]{ |
240 |
new UserMessage |
241 |
{ |
242 |
Message = e.Exception.Message, |
243 |
Details = e.Exception.ToString(), |
244 |
Severity = Severity.Error |
245 |
} |
246 |
}; |
247 |
ShowMessages(WPF.Properties.Resources.Error_Title, |
248 |
WPF.Properties.Resources.Unexpected_Error, |
249 |
messages); |
250 |
e.Handled=true; |
251 |
} |
252 |
|
253 |
void ShowMessages(string title,string message,IEnumerable<UserMessage> messages ) |
254 |
{ |
255 |
var messageList = messages.ToList(); |
256 |
LogMessages(messageList); |
257 |
Execute.OnUIThread(()=>{ |
258 |
var messageView = new MessageView(messageList) |
259 |
{ |
260 |
Title = title, |
261 |
Message = message |
262 |
}; |
263 |
messageView.ShowDialog(); |
264 |
}); |
265 |
} |
266 |
|
267 |
private void LogMessages(IEnumerable<UserMessage> messages) |
268 |
{ |
269 |
var logMessage = CreateMessage(messages); |
270 |
|
271 |
Log.Error(logMessage); |
272 |
} |
273 |
|
274 |
private static string CreateMessage(IEnumerable<UserMessage> messages) |
275 |
{ |
276 |
var messageBuilder = messages.Aggregate(new StringBuilder("Unexpected Error\r\n"), |
277 |
(builder, message) => |
278 |
{ |
279 |
builder.AppendFormat("\r\n[{0}] {1}\r\n{2}\r\n", message.Severity, |
280 |
message.Message, message.Details); |
281 |
return builder; |
282 |
}); |
283 |
var logMessage = messageBuilder.ToString(); |
284 |
return logMessage; |
285 |
} |
286 |
} |
287 |
|
288 |
} |