Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / App.xaml.cs @ 20e9a378

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 Pithos.Client.WPF.Caliburn.Micro.Logging;
15

    
16

    
17
namespace Pithos.Client.WPF
18
{
19
    /// <summary>
20
    /// Interaction logic for App.xaml
21
    /// </summary>
22
    public partial class App : Application
23
    {
24
        private log4net.ILog Log = log4net.LogManager.GetLogger(typeof (App));
25
        public App()
26
        {
27
            //var extensionController = new ShellExtensionController();
28
            //extensionController.RegisterExtensions();
29
            log4net.Config.XmlConfigurator.Configure();            
30

    
31
            this.DispatcherUnhandledException += OnUnhandledException;
32
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
33
            TaskScheduler.UnobservedTaskException += OnUnobservedException;
34

    
35
            var appPatth =    Assembly.GetExecutingAssembly().Location;
36
            Registry.LocalMachine.SetValue(@"Software\Pithos\AppPath",appPatth );            
37
            InitializeComponent();            
38
            //Application.Current.ApplyTheme("BureauBlue");
39
        }
40

    
41
        private void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e)
42
        {            
43
            var messages=new List<UserMessage>();
44
            e.Exception.Handle(exc=>{
45
                messages.Add(new UserMessage
46
                {
47
                    Message = "Unexpected Exception",
48
                    Details = exc.ToString(),
49
                    Severity = Severity.Error
50
                });
51
                return true;
52
            });
53

    
54
            ShowMessages("Oops!","Where did that come from? \r\nErm, an exception occured.\r\nWe apologize for the inconvenience", messages);
55
            e.SetObserved();
56
        }
57

    
58
        private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
59
        {
60
            var message = e.IsTerminating
61
                              ? "Unexpected Exception. The application must terminate"
62
                              : "Unexpected Exception";
63

    
64

    
65
            var messages = new[]{
66
                                   new UserMessage
67
                                       {
68
                                           Message = (e.ExceptionObject as Exception).Message,
69
                                           Details = e.ExceptionObject.ToString(),
70
                                           Severity = Severity.Error
71
                                       }
72
                               };
73
            ShowMessages("Oops!", message,messages);
74
        }
75

    
76
        void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
77
        {                        
78
            var messages = new[]{
79
                                   new UserMessage
80
                                       {
81
                                           Message = e.Exception.Message, 
82
                                           Details = e.Exception.ToString(),
83
                                           Severity = Severity.Error
84
                                       }
85
                               };
86
            ShowMessages("Oops!", "Unexcpected Exception", messages);
87
            e.Handled=true;
88
        }
89

    
90
        void ShowMessages(string title,string message,IEnumerable<UserMessage> messages )
91
        {
92
            LogMessages(messages);
93
            Execute.OnUIThread(()=>{
94
                var messageView = new MessageView(messages);
95
                messageView.Title = title;
96
                messageView.Message = message;
97
                messageView.ShowDialog();
98
            });
99
        }
100

    
101
        private void LogMessages(IEnumerable<UserMessage> messages)
102
        {
103
            var logMessage = CreateMessage(messages);
104

    
105
            Log.Error(logMessage);
106
        }
107

    
108
        private async void SendMessages(IEnumerable<UserMessage> messages )
109
        {
110
            var logMessage = CreateMessage(messages);
111

    
112
            var mailer = new SmtpClient();
113
            await mailer.SendTaskAsync("pithos@grnet.gr", "support@pithos.grnet.gr", "Errors", logMessage);
114
        }
115

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

    
130
}