Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / App.xaml.cs @ f327ad2b

History | View | Annotate | Download (5.1 kB)

1
´╗┐using System;
2
using System.Collections.Generic;
3
using System.Configuration;
4
using System.Data;
5
using System.Diagnostics;
6
using System.Linq;
7
using System.Net;
8
using System.Net.Mail;
9
using System.Reflection;
10
using System.Text;
11
using System.Threading.Tasks;
12
using System.Windows;
13
using Caliburn.Micro;
14
using Microsoft.Win32;
15
using Caliburn.Micro.Logging;
16
using Pithos.Client.WPF.Properties;
17

    
18

    
19
namespace Pithos.Client.WPF
20
{
21
    /// <summary>
22
    /// Interaction logic for App.xaml
23
    /// </summary>
24
    public partial class App : Application
25
    {
26
        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof (App));
27

    
28
        public App()
29
        {
30
            log4net.Config.XmlConfigurator.Configure();            
31
            
32
            this.DispatcherUnhandledException += OnUnhandledException;
33
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
34
            TaskScheduler.UnobservedTaskException += OnUnobservedException;
35

    
36
            //Fix incorrect proxy settings by switching to default proxy, if the proxy server settings is empty
37
            if (Settings.Default.UseManualProxy && String.IsNullOrWhiteSpace(Settings.Default.ProxyServer))
38
            {
39
                Settings.Default.UseManualProxy = false;
40
                Settings.Default.UseDefaultProxy = true;
41
            }
42

    
43
            InitializeComponent();            
44
        }
45

    
46
        protected override void OnStartup(StartupEventArgs e)
47
        {            
48
            if (!Settings.Default.StartOnSystemStartup && e.Args.Contains("startup"))
49
            {
50
                this.Shutdown();
51
            }
52
            base.OnStartup(e);
53
        }
54

    
55
        private void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e)
56
        {            
57
            var messages=new List<UserMessage>();
58
            e.Exception.Handle(exc=>{
59
                messages.Add(new UserMessage
60
                {
61
                    Message = "Unexpected Exception",
62
                    Details = exc.ToString(),
63
                    Severity = Severity.Error
64
                });
65
                return true;
66
            });
67

    
68
            ShowMessages("Oops!","Where did that come from? \r\nErm, an exception occured.\r\nWe apologize for the inconvenience", messages);
69
            e.SetObserved();
70
        }
71

    
72
        private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
73
        {
74
            var message = e.IsTerminating
75
                              ? "Unexpected Exception. The application must terminate"
76
                              : "Unexpected Exception";
77

    
78

    
79
            var messages = new[]{
80
                                   new UserMessage
81
                                       {
82
                                           Message = (e.ExceptionObject as Exception).Message,
83
                                           Details = e.ExceptionObject.ToString(),
84
                                           Severity = Severity.Error
85
                                       }
86
                               };
87
            ShowMessages("Oops!", message,messages);
88
        }
89

    
90
        void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
91
        {                        
92
            var messages = new[]{
93
                                   new UserMessage
94
                                       {
95
                                           Message = e.Exception.Message, 
96
                                           Details = e.Exception.ToString(),
97
                                           Severity = Severity.Error
98
                                       }
99
                               };
100
            ShowMessages("Oops!", "Unexcpected Exception", messages);
101
            e.Handled=true;
102
        }
103

    
104
        void ShowMessages(string title,string message,IEnumerable<UserMessage> messages )
105
        {
106
            LogMessages(messages);
107
            Execute.OnUIThread(()=>{
108
                var messageView = new MessageView(messages);
109
                messageView.Title = title;
110
                messageView.Message = message;
111
                messageView.ShowDialog();
112
            });
113
        }
114

    
115
        private void LogMessages(IEnumerable<UserMessage> messages)
116
        {
117
            var logMessage = CreateMessage(messages);
118

    
119
            _log.Error(logMessage);
120
        }
121

    
122
        private static string CreateMessage(IEnumerable<UserMessage> messages)
123
        {
124
            var messageBuilder = messages.Aggregate(new StringBuilder("Unexpected Error\r\n"),
125
                                                    (builder, message) =>
126
                                                        {
127
                                                            builder.AppendFormat("\r\n[{0}] {1}\r\n{2}\r\n", message.Severity,
128
                                                                                 message.Message, message.Details);
129
                                                            return builder;
130
                                                        });
131
            var logMessage = messageBuilder.ToString();
132
            return logMessage;
133
        }
134
    }
135

    
136
}