-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
114 lines (90 loc) · 3.83 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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using log4net;
using log4net.Appender;
namespace Inventory
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static readonly ILog Log = LogManager.GetLogger(typeof(App));
protected override void OnStartup(StartupEventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
log4net.Repository.Hierarchy.Hierarchy h =
(log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
foreach (IAppender a in h.Root.Appenders)
{
if (a is FileAppender)
{
FileAppender fa = (FileAppender)a;
// Programmatically set this to the desired location here
string logFileLocation = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\InventoryDBA\log.txt";
// Uncomment the lines below if you want to retain the base file name
// and change the folder name...
//FileInfo fileInfo = new FileInfo(fa.File);
//logFileLocation = string.Format(@"C:\MySpecialFolder\{0}", fileInfo.Name);
fa.File = logFileLocation;
fa.ActivateOptions();
break;
}
}
}
public App()
{
Log.Info("Application start");
Log.Info("After CopyDatabase Function");
}
public bool DoHandle { get; set; }
private void Application_DispatcherUnhandledException(object sender,
System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "Uncaught Exception");
if (e.Exception.InnerException != null)
{
MessageBox.Show(e.Exception.InnerException.Message, "Inner Exception");
}
MessageBox.Show(e.Exception.StackTrace.ToString(), "Stack Trace Exception");
if (this.DoHandle)
{
//Handling the exception within the UnhandledException handler.
MessageBox.Show(e.Exception.Message, "Exception Caught",
MessageBoxButton.OK, MessageBoxImage.Error);
e.Handled = true;
}
else
{
//If you do not set e.Handled to true, the application will close due to crash.
MessageBox.Show("Application is going to close! ", "Uncaught Exception");
e.Handled = false;
}
}
private void Application_Startup(object sender, StartupEventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
Log.Error(ex.Message);
MessageBox.Show(ex.Message, "Uncaught Thread Exception",
MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show(ex.InnerException.Message, "Uncaught Thread Exception",
MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show(ex.InnerException.ToString(), "Uncaught Thread Exception",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}