Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / App.xaml.cs @ 8a84d039

History | View | Annotate | Download (5 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.Mail;
8
using System.Reflection;
9
using System.Text;
10
using System.Threading.Tasks;
11
using System.Windows;
12
using Caliburn.Micro;
13
using Microsoft.Win32;
14
using Caliburn.Micro.Logging;
15
using Pithos.Client.WPF.Properties;
16

    
17

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

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

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

    
42
            InitializeComponent();            
43
        }
44

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

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

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

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

    
77

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

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

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

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

    
118
            _log.Error(logMessage);
119
        }
120

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

    
135
}