Skip to content

Commit aa156fd

Browse files
Initial version of Serilog.Enrichers.ExcelDna with a sample
1 parent ec47aab commit aa156fd

19 files changed

+853
-0
lines changed

sample/SampleAddIn/AddIn.cs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright 2018 Caio Proiete & Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using System.Threading;
17+
using System.Threading.Tasks;
18+
using System.Windows.Forms;
19+
using ExcelDna.Integration;
20+
using ExcelDna.Integration.Extensibility;
21+
using ExcelDna.Logging;
22+
using Serilog;
23+
24+
namespace SampleAddIn
25+
{
26+
public class AddIn : ExcelComAddIn, IExcelAddIn
27+
{
28+
private static ILogger _log = Log.Logger;
29+
30+
public void AutoOpen()
31+
{
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()
114+
.MinimumLevel.Verbose()
115+
.WriteTo.ExcelDnaLogDisplay(displayOrder: DisplayOrder.NewestFirst,
116+
outputTemplate: "{Properties:j}{NewLine}[{Level:u3}] {Message:lj}{NewLine}{Exception}")
117+
.Enrich.WithXllPath()
118+
.Enrich.WithExcelVersion()
119+
.Enrich.WithExcelVersionName()
120+
.Enrich.WithExcelBitness()
121+
.CreateLogger();
122+
}
123+
124+
private static void ApplicationThreadUnhandledException(object sender, ThreadExceptionEventArgs e)
125+
{
126+
ProcessUnhandledException(e.Exception);
127+
}
128+
129+
private static void TaskSchedulerUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
130+
{
131+
ProcessUnhandledException(e.Exception);
132+
e.SetObserved();
133+
}
134+
135+
private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
136+
{
137+
ProcessUnhandledException((Exception)e.ExceptionObject);
138+
}
139+
140+
private static object ExcelUnhandledException(object ex)
141+
{
142+
ProcessUnhandledException((Exception)ex);
143+
return ex;
144+
}
145+
}
146+
}

sample/SampleAddIn/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("SampleAddIn")]
8+
[assembly: AssemblyDescription("")]
9+
10+
#if DEBUG
11+
[assembly: AssemblyConfiguration("Debug")]
12+
#else
13+
[assembly: AssemblyConfiguration("Release")]
14+
#endif
15+
16+
[assembly: AssemblyCompany("caioproiete.net")]
17+
[assembly: AssemblyProduct("Serilog.Enrichers.ExcelDna")]
18+
[assembly: AssemblyCopyright("Copyright © 2018 Caio Proiete & Contributors")]
19+
[assembly: AssemblyTrademark("")]
20+
[assembly: AssemblyCulture("")]
21+
22+
// Setting ComVisible to false makes the types in this assembly not visible
23+
// to COM components. If you need to access a type in this assembly from
24+
// COM, set the ComVisible attribute to true on that type.
25+
[assembly: ComVisible(false)]
26+
27+
// The following GUID is for the ID of the typelib if this project is exposed to COM
28+
[assembly: Guid("cec178ab-3a3a-49b4-8064-d8523e0d3735")]
29+
30+
// Version information for an assembly consists of the following four values:
31+
//
32+
// Major Version
33+
// Minor Version
34+
// Build Number
35+
// Revision
36+
//
37+
// You can specify all the values or you can default the Build and Revision Numbers
38+
// by using the '*' as shown below:
39+
// [assembly: AssemblyVersion("1.0.*")]
40+
[assembly: AssemblyVersion("1.0.0.0")]
41+
[assembly: AssemblyFileVersion("1.0.0.0")]
42+
[assembly: AssemblyInformationalVersion("1.0.0")]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="ExcelDnaProps">
3+
<!--
4+
If you change properties in this file, they may not come into effect until you:
5+
* Rebuild the solution/project
6+
7+
or
8+
9+
* Close Visual Studio
10+
* Delete .vs folder, if exists
11+
* Delete ProjectName.csproj.user (or equivalent for VB, F#, etc.), if exists
12+
* Delete SolutionName.suo, if exists
13+
* Open your solution/project again in Visual Studio
14+
-->
15+
16+
<!--
17+
Configuration properties for building .dna files
18+
-->
19+
<PropertyGroup>
20+
<!--
21+
Enable/Disable automatic generation of platform-specific versions of .dna files
22+
-->
23+
<ExcelDnaCreate32BitAddIn Condition="'$(ExcelDnaCreate32BitAddIn)' == ''">true</ExcelDnaCreate32BitAddIn>
24+
<ExcelDnaCreate64BitAddIn Condition="'$(ExcelDnaCreate64BitAddIn)' == ''">true</ExcelDnaCreate64BitAddIn>
25+
26+
<!--
27+
Define the suffix used for each platform-specific file e.g. MyAddIn64.dna
28+
-->
29+
<ExcelDna32BitAddInSuffix Condition="'$(ExcelDna32BitAddInSuffix)' == ''">32</ExcelDna32BitAddInSuffix>
30+
<ExcelDna64BitAddInSuffix Condition="'$(ExcelDna64BitAddInSuffix)' == ''">64</ExcelDna64BitAddInSuffix>
31+
</PropertyGroup>
32+
33+
<!--
34+
Configuration properties for packing .dna files
35+
-->
36+
<PropertyGroup>
37+
<!--
38+
Enable/Disable packing of .dna files
39+
-->
40+
<RunExcelDnaPack Condition="'$(RunExcelDnaPack)' == ''">false</RunExcelDnaPack>
41+
42+
<!--
43+
Suffix used for packed .xll files e.g. MyAddIn-packed.xll
44+
-->
45+
<ExcelDnaPackXllSuffix Condition="'$(ExcelDnaPackXllSuffix)' == ''">-packed</ExcelDnaPackXllSuffix>
46+
</PropertyGroup>
47+
</Project>
48+

sample/SampleAddIn/SampleAddIn.csproj

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{CEC178AB-3A3A-49B4-8064-D8523E0D3735}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>SampleAddIn</RootNamespace>
11+
<AssemblyName>SampleAddIn</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
33+
<DocumentationFile>
34+
</DocumentationFile>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="ExcelDna.Integration, Version=0.34.6373.42344, Culture=neutral, processorArchitecture=MSIL">
38+
<HintPath>..\..\packages\ExcelDna.Integration.0.34.6\lib\ExcelDna.Integration.dll</HintPath>
39+
<Private>False</Private>
40+
</Reference>
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
43+
<HintPath>..\..\packages\Serilog.2.7.1\lib\net45\Serilog.dll</HintPath>
44+
</Reference>
45+
<Reference Include="Serilog.Sinks.ExcelDnaLogDisplay, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
46+
<HintPath>..\..\packages\Serilog.Sinks.ExcelDnaLogDisplay.1.0.0\lib\net45\Serilog.Sinks.ExcelDnaLogDisplay.dll</HintPath>
47+
</Reference>
48+
<Reference Include="System" />
49+
<Reference Include="System.Core" />
50+
<Reference Include="System.Windows.Forms" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<Compile Include="AddIn.cs" />
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<None Include="App.config">
58+
<SubType>Designer</SubType>
59+
</None>
60+
<None Include="packages.config" />
61+
<None Include="SampleAddIn.dna" />
62+
<None Include="Properties\ExcelDna.Build.props" />
63+
</ItemGroup>
64+
<ItemGroup>
65+
<ProjectReference Include="..\..\src\Serilog.Enrichers.ExcelDna\Serilog.Enrichers.ExcelDna.csproj">
66+
<Project>{5f26ef43-543e-48f2-8b83-52ea92f8e73c}</Project>
67+
<Name>Serilog.Enrichers.ExcelDna</Name>
68+
</ProjectReference>
69+
</ItemGroup>
70+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
71+
<Import Project="..\..\packages\ExcelDna.AddIn.0.34.6\tools\ExcelDna.AddIn.targets" Condition="Exists('..\..\packages\ExcelDna.AddIn.0.34.6\tools\ExcelDna.AddIn.targets')" />
72+
<Target Name="EnsureExcelDnaTargetsImported" BeforeTargets="BeforeBuild" Condition="'$(ExcelDnaTargetsImported)' == ''">
73+
<Error Condition="!Exists('..\..\packages\ExcelDna.AddIn.0.34.6\tools\ExcelDna.AddIn.targets') And ('$(RunExcelDnaBuild)' != '' And $(RunExcelDnaBuild))" Text="You are trying to build with ExcelDna, but the NuGet targets file that ExcelDna depends on is not available on this computer. This is probably because the ExcelDna package has not been committed to source control, or NuGet Package Restore is not enabled. Please enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
74+
<Error Condition="Exists('..\..\packages\ExcelDna.AddIn.0.34.6\tools\ExcelDna.AddIn.targets') And ('$(RunExcelDnaBuild)' != '' And $(RunExcelDnaBuild))" Text="ExcelDna cannot be run because NuGet packages were restored prior to the build running, and the targets file was unavailable when the build started. Please build the project again to include these packages in the build. You may also need to make sure that your build server does not delete packages prior to each build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
75+
</Target>
76+
</Project>

sample/SampleAddIn/SampleAddIn.dna

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<DnaLibrary Name="Sample Add-In" RuntimeVersion="v4.0">
2+
<ExternalLibrary Path="SampleAddIn.dll" ExplicitExports="true" ExplicitRegistration="true" LoadFromBytes="false" Pack="false" />
3+
</DnaLibrary>

sample/SampleAddIn/packages.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="ExcelDna.AddIn" version="0.34.6" targetFramework="net45" />
4+
<package id="ExcelDna.Integration" version="0.34.6" targetFramework="net45" />
5+
<package id="Serilog" version="2.7.1" targetFramework="net45" />
6+
<package id="Serilog.Sinks.ExcelDnaLogDisplay" version="1.0.0" targetFramework="net45" />
7+
</packages>

serilog-enrichers-exceldna.sln

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28010.2046
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{FC355FE6-D157-4D77-985C-8F4F0078C607}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{032590B4-A924-488D-88D9-2636B0CDE75C}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{1FB7904B-47AF-418B-8567-2CE55457A549}"
11+
ProjectSection(SolutionItems) = preProject
12+
.editorconfig = .editorconfig
13+
.gitattributes = .gitattributes
14+
.gitignore = .gitignore
15+
CHANGES.md = CHANGES.md
16+
LICENSE = LICENSE
17+
README.md = README.md
18+
EndProjectSection
19+
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleAddIn", "sample\SampleAddIn\SampleAddIn.csproj", "{CEC178AB-3A3A-49B4-8064-D8523E0D3735}"
21+
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Enrichers.ExcelDna", "src\Serilog.Enrichers.ExcelDna\Serilog.Enrichers.ExcelDna.csproj", "{5F26EF43-543E-48F2-8B83-52EA92F8E73C}"
23+
EndProject
24+
Global
25+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
26+
Debug|Any CPU = Debug|Any CPU
27+
Release|Any CPU = Release|Any CPU
28+
EndGlobalSection
29+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
30+
{CEC178AB-3A3A-49B4-8064-D8523E0D3735}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{CEC178AB-3A3A-49B4-8064-D8523E0D3735}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{CEC178AB-3A3A-49B4-8064-D8523E0D3735}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{CEC178AB-3A3A-49B4-8064-D8523E0D3735}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{5F26EF43-543E-48F2-8B83-52EA92F8E73C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{5F26EF43-543E-48F2-8B83-52EA92F8E73C}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{5F26EF43-543E-48F2-8B83-52EA92F8E73C}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{5F26EF43-543E-48F2-8B83-52EA92F8E73C}.Release|Any CPU.Build.0 = Release|Any CPU
38+
EndGlobalSection
39+
GlobalSection(SolutionProperties) = preSolution
40+
HideSolutionNode = FALSE
41+
EndGlobalSection
42+
GlobalSection(NestedProjects) = preSolution
43+
{CEC178AB-3A3A-49B4-8064-D8523E0D3735} = {FC355FE6-D157-4D77-985C-8F4F0078C607}
44+
{5F26EF43-543E-48F2-8B83-52EA92F8E73C} = {032590B4-A924-488D-88D9-2636B0CDE75C}
45+
EndGlobalSection
46+
GlobalSection(ExtensibilityGlobals) = postSolution
47+
SolutionGuid = {CC318D6F-08B7-4132-9850-468362A7B630}
48+
EndGlobalSection
49+
EndGlobal

0 commit comments

Comments
 (0)