13
13
// limitations under the License.
14
14
15
15
using System ;
16
- using System . Threading ;
17
- using System . Threading . Tasks ;
18
- using System . Windows . Forms ;
19
16
using ExcelDna . Integration ;
20
17
using ExcelDna . Integration . Extensibility ;
21
18
using ExcelDna . Logging ;
@@ -25,122 +22,35 @@ namespace SampleAddIn
25
22
{
26
23
public class AddIn : ExcelComAddIn , IExcelAddIn
27
24
{
28
- private static ILogger _log = Log . Logger ;
29
-
30
25
public void AutoOpen ( )
31
26
{
32
- try
33
- {
34
- Application . ThreadException += ApplicationThreadUnhandledException ;
35
- AppDomain . CurrentDomain . UnhandledException += AppDomainUnhandledException ;
36
- TaskScheduler . UnobservedTaskException += TaskSchedulerUnobservedTaskException ;
37
- ExcelIntegration . RegisterUnhandledExceptionHandler ( ExcelUnhandledException ) ;
38
-
39
- _log = Log . Logger = ConfigureLogging ( ) ;
40
- _log . Information ( "Starting sample Excel-DNA Add-In with Serilog Enricher ExcelDna" ) ;
41
-
42
- LogDisplay . Show ( ) ;
43
-
44
- _log . Information ( "Sample Excel-DNA Add-In with Serilog Enricher ExcelDna started" ) ;
45
- }
46
- catch ( Exception ex )
47
- {
48
- ProcessUnhandledException ( ex ) ;
49
- }
50
- }
51
-
52
- public void AutoClose ( )
53
- {
54
- // Do nothing
55
- }
56
-
57
- public override void OnDisconnection ( ext_DisconnectMode disconnectMode , ref Array custom )
58
- {
59
- try
60
- {
61
- base . OnDisconnection ( disconnectMode , ref custom ) ;
62
-
63
- _log . Information ( "Stopping sample Excel-DNA Add-In with Serilog Sink LogDisplay" ) ;
64
- }
65
- catch ( Exception ex )
66
- {
67
- ProcessUnhandledException ( ex ) ;
68
- }
69
- finally
70
- {
71
- _log . Information ( "Sample Excel-DNA Add-In with Serilog Sink LogDisplay stopped" ) ;
72
-
73
- Log . CloseAndFlush ( ) ;
74
- }
75
- }
76
-
77
- public static void ProcessUnhandledException ( Exception ex )
78
- {
79
- try
80
- {
81
- _log . Error ( ex , null ) ;
82
- }
83
- catch ( Exception lex )
84
- {
85
- try
86
- {
87
- Serilog . Debugging . SelfLog . WriteLine ( lex . ToString ( ) ) ;
88
- }
89
- catch
90
- {
91
- // Do nothing...
92
- }
93
- }
94
-
95
- if ( ex . InnerException != null )
96
- {
97
- ProcessUnhandledException ( ex . InnerException ) ;
98
- return ;
99
- }
100
-
101
- #if DEBUG
102
- MessageBox . Show ( ex . ToString ( ) , "Unexpected Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
103
- #else
104
- const string errorMessage = "An unexpected error ocurred. Please try again in a few minutes, and if the error persists, contact support" ;
105
- MessageBox . Show ( errorMessage , "Unexpected Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
106
- #endif
107
- }
108
-
109
- private static ILogger ConfigureLogging ( )
110
- {
111
- AppDomain . CurrentDomain . ProcessExit += ( sender , args ) => Log . CloseAndFlush ( ) ;
112
-
113
- return new LoggerConfiguration ( )
27
+ Log . Logger = new LoggerConfiguration ( )
114
28
. MinimumLevel . Verbose ( )
115
- . WriteTo . ExcelDnaLogDisplay ( displayOrder : DisplayOrder . NewestFirst ,
116
- outputTemplate : "{Properties:j}{NewLine}[{Level:u3}] {Message:lj}{NewLine}{Exception}" )
117
29
. Enrich . WithXllPath ( )
118
30
. Enrich . WithExcelVersion ( )
119
31
. Enrich . WithExcelVersionName ( )
120
32
. Enrich . WithExcelBitness ( )
33
+ . WriteTo . ExcelDnaLogDisplay ( displayOrder : DisplayOrder . NewestFirst ,
34
+ outputTemplate : "{Properties:j}{NewLine}[{Level:u3}] {Message:lj}{NewLine}{Exception}" )
121
35
. CreateLogger ( ) ;
122
- }
123
36
124
- private static void ApplicationThreadUnhandledException ( object sender , ThreadExceptionEventArgs e )
125
- {
126
- ProcessUnhandledException ( e . Exception ) ;
127
- }
37
+ Log . Information ( "Hello from {AddInName}! :)" , DnaLibrary . CurrentLibrary . Name ) ;
38
+ LogDisplay . Show ( ) ;
128
39
129
- private static void TaskSchedulerUnobservedTaskException ( object sender , UnobservedTaskExceptionEventArgs e )
130
- {
131
- ProcessUnhandledException ( e . Exception ) ;
132
- e . SetObserved ( ) ;
40
+ ExcelComAddInHelper . LoadComAddIn ( this ) ;
133
41
}
134
42
135
- private static void AppDomainUnhandledException ( object sender , UnhandledExceptionEventArgs e )
43
+ public void AutoClose ( )
136
44
{
137
- ProcessUnhandledException ( ( Exception ) e . ExceptionObject ) ;
45
+ // Do nothing
138
46
}
139
47
140
- private static object ExcelUnhandledException ( object ex )
48
+ public override void OnDisconnection ( ext_DisconnectMode disconnectMode , ref Array custom )
141
49
{
142
- ProcessUnhandledException ( ( Exception ) ex ) ;
143
- return ex ;
50
+ Log . Information ( "Goodbye from {AddInName}! :)" , DnaLibrary . CurrentLibrary . Name ) ;
51
+ Log . CloseAndFlush ( ) ;
52
+
53
+ base . OnDisconnection ( disconnectMode , ref custom ) ;
144
54
}
145
55
}
146
56
}
0 commit comments