Skip to content

Commit 362dfa4

Browse files
Simplify Sample Add-In to focus on log enrichment only
1 parent 0b8f4cd commit 362dfa4

File tree

3 files changed

+14
-105
lines changed

3 files changed

+14
-105
lines changed

sample/SampleAddIn/AddIn.cs

Lines changed: 13 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
// limitations under the License.
1414

1515
using System;
16-
using System.Threading;
17-
using System.Threading.Tasks;
18-
using System.Windows.Forms;
1916
using ExcelDna.Integration;
2017
using ExcelDna.Integration.Extensibility;
2118
using ExcelDna.Logging;
@@ -25,122 +22,35 @@ namespace SampleAddIn
2522
{
2623
public class AddIn : ExcelComAddIn, IExcelAddIn
2724
{
28-
private static ILogger _log = Log.Logger;
29-
3025
public void AutoOpen()
3126
{
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()
11428
.MinimumLevel.Verbose()
115-
.WriteTo.ExcelDnaLogDisplay(displayOrder: DisplayOrder.NewestFirst,
116-
outputTemplate: "{Properties:j}{NewLine}[{Level:u3}] {Message:lj}{NewLine}{Exception}")
11729
.Enrich.WithXllPath()
11830
.Enrich.WithExcelVersion()
11931
.Enrich.WithExcelVersionName()
12032
.Enrich.WithExcelBitness()
33+
.WriteTo.ExcelDnaLogDisplay(displayOrder: DisplayOrder.NewestFirst,
34+
outputTemplate: "{Properties:j}{NewLine}[{Level:u3}] {Message:lj}{NewLine}{Exception}")
12135
.CreateLogger();
122-
}
12336

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();
12839

129-
private static void TaskSchedulerUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
130-
{
131-
ProcessUnhandledException(e.Exception);
132-
e.SetObserved();
40+
ExcelComAddInHelper.LoadComAddIn(this);
13341
}
13442

135-
private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
43+
public void AutoClose()
13644
{
137-
ProcessUnhandledException((Exception)e.ExceptionObject);
45+
// Do nothing
13846
}
13947

140-
private static object ExcelUnhandledException(object ex)
48+
public override void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
14149
{
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);
14454
}
14555
}
14656
}

sample/SampleAddIn/SampleAddIn.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
</Reference>
5050
<Reference Include="System" />
5151
<Reference Include="System.Core" />
52-
<Reference Include="System.Windows.Forms" />
5352
</ItemGroup>
5453
<ItemGroup>
5554
<Compile Include="AddIn.cs" />

sample/SampleAddIn/SampleAddIn.dna

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<DnaLibrary Name="Sample Add-In" RuntimeVersion="v4.0" xmlns="http://schemas.excel-dna.net/addin/2018/05/dnalibrary">
1+
<DnaLibrary Name="Excel-DNA Enrichers Sample Add-In" RuntimeVersion="v4.0" xmlns="http://schemas.excel-dna.net/addin/2018/05/dnalibrary">
22
<ExternalLibrary Path="SampleAddIn.dll" ExplicitExports="true" ExplicitRegistration="true" LoadFromBytes="false" Pack="false" />
33
</DnaLibrary>

0 commit comments

Comments
 (0)