77using System . Linq ;
88using System . Windows ;
99using Caliburn . Micro ;
10+ using SimpleDnsCrypt . Helper ;
1011using SimpleDnsCrypt . Logger ;
1112using SimpleDnsCrypt . ViewModels ;
1213
1314namespace SimpleDnsCrypt {
1415
1516 public class AppBootstrapper : BootstrapperBase {
1617 private CompositionContainer _container ;
18+ private static readonly ILog Log = LogManagerHelper . Factory ( ) ;
1719
18- public AppBootstrapper ( ) {
20+ public AppBootstrapper ( ) {
1921 LogManager . GetLog = type => new NLogLogger ( type ) ;
2022 Initialize ( ) ;
2123 }
2224
2325 protected override void Configure ( ) {
24- _container = new CompositionContainer (
25- new AggregateCatalog ( AssemblySource . Instance . Select ( x => new AssemblyCatalog ( x ) ) . OfType < ComposablePartCatalog > ( ) )
26- ) ;
27- var batch = new CompositionBatch ( ) ;
28- batch . AddExportedValue < IWindowManager > ( new AppWindowManager ( ) ) ;
29- batch . AddExportedValue < IEventAggregator > ( new EventAggregator ( ) ) ;
30- batch . AddExportedValue ( _container ) ;
31- _container . Compose ( batch ) ;
26+ try
27+ {
28+ _container = new CompositionContainer (
29+ new AggregateCatalog ( AssemblySource . Instance . Select ( x => new AssemblyCatalog ( x ) )
30+ . OfType < ComposablePartCatalog > ( ) )
31+ ) ;
32+ var batch = new CompositionBatch ( ) ;
33+ batch . AddExportedValue < IWindowManager > ( new AppWindowManager ( ) ) ;
34+ batch . AddExportedValue < IEventAggregator > ( new EventAggregator ( ) ) ;
35+ batch . AddExportedValue ( _container ) ;
36+ _container . Compose ( batch ) ;
37+ }
38+ catch ( Exception exception )
39+ {
40+ Log . Error ( exception ) ;
41+ }
3242 }
3343
34- protected override object GetInstance ( Type service , string key ) {
35- var contract = string . IsNullOrEmpty ( key ) ? AttributedModelServices . GetContractName ( service ) : key ;
36- var exports = _container . GetExportedValues < object > ( contract ) ;
44+ protected override object GetInstance ( Type service , string key )
45+ {
46+ var contract = string . IsNullOrEmpty ( key ) ? AttributedModelServices . GetContractName ( service ) : key ;
47+ var exports = _container . GetExportedValues < object > ( contract ) ;
3748
38- if ( exports . Any ( ) ) return exports . First ( ) ;
39- throw new Exception ( $ "Could not locate any instances of contract { contract } .") ;
40- }
49+ if ( exports . Any ( ) )
50+ {
51+ return exports . First ( ) ;
52+ }
53+ throw new Exception ( $ "Could not locate any instances of contract { contract } .") ;
54+ }
4155
4256 protected override IEnumerable < object > GetAllInstances ( Type service ) {
4357 return _container . GetExportedValues < object > ( AttributedModelServices . GetContractName ( service ) ) ;
@@ -49,25 +63,33 @@ protected override void BuildUp(object instance) {
4963
5064 protected override void OnStartup ( object sender , StartupEventArgs e )
5165 {
52- if ( e . Args . Length == 1 )
53- {
54- if ( e . Args [ 0 ] . Equals ( "-debug" ) )
55- {
56- LogMode . Debug = true ;
57- }
58- }
59- var loader = _container . GetExportedValue < LoaderViewModel > ( ) ;
60- var windowManager = IoC . Get < IWindowManager > ( ) ;
61- windowManager . ShowDialog ( loader ) ;
62- }
66+ try
67+ {
68+ if ( e . Args . Length == 1 )
69+ {
70+ if ( e . Args [ 0 ] . Equals ( "-debug" ) )
71+ {
72+ LogMode . Debug = true ;
73+ }
74+ }
75+
76+ var loader = _container . GetExportedValue < LoaderViewModel > ( ) ;
77+ var windowManager = IoC . Get < IWindowManager > ( ) ;
78+ windowManager . ShowDialog ( loader ) ;
79+ }
80+ catch ( Exception exception )
81+ {
82+ Log . Error ( exception ) ;
83+ }
84+ }
6385
6486 protected override void OnUnhandledException ( object sender , DispatcherUnhandledExceptionEventArgs e )
6587 {
66-
88+ Log . Error ( e . Exception ) ;
6789 Execute . OnUIThread (
6890 ( ) =>
6991 MessageBox . Show (
70- $ "Message: { e . Exception . Message } \n StackTrace: { e . Exception . StackTrace } ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ) ;
92+ "There was an UnhandledException. Check the log entries for further information. ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ) ;
7193 }
7294 }
7395}
0 commit comments