-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial version of Serilog.Enrichers.ExcelDna with a sample
- Loading branch information
1 parent
ec47aab
commit aa156fd
Showing
19 changed files
with
853 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright 2018 Caio Proiete & Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
using ExcelDna.Integration; | ||
using ExcelDna.Integration.Extensibility; | ||
using ExcelDna.Logging; | ||
using Serilog; | ||
|
||
namespace SampleAddIn | ||
{ | ||
public class AddIn : ExcelComAddIn, IExcelAddIn | ||
{ | ||
private static ILogger _log = Log.Logger; | ||
|
||
public void AutoOpen() | ||
{ | ||
try | ||
{ | ||
Application.ThreadException += ApplicationThreadUnhandledException; | ||
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException; | ||
TaskScheduler.UnobservedTaskException += TaskSchedulerUnobservedTaskException; | ||
ExcelIntegration.RegisterUnhandledExceptionHandler(ExcelUnhandledException); | ||
|
||
_log = Log.Logger = ConfigureLogging(); | ||
_log.Information("Starting sample Excel-DNA Add-In with Serilog Enricher ExcelDna"); | ||
|
||
LogDisplay.Show(); | ||
|
||
_log.Information("Sample Excel-DNA Add-In with Serilog Enricher ExcelDna started"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
ProcessUnhandledException(ex); | ||
} | ||
} | ||
|
||
public void AutoClose() | ||
{ | ||
// Do nothing | ||
} | ||
|
||
public override void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) | ||
{ | ||
try | ||
{ | ||
base.OnDisconnection(disconnectMode, ref custom); | ||
|
||
_log.Information("Stopping sample Excel-DNA Add-In with Serilog Sink LogDisplay"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
ProcessUnhandledException(ex); | ||
} | ||
finally | ||
{ | ||
_log.Information("Sample Excel-DNA Add-In with Serilog Sink LogDisplay stopped"); | ||
|
||
Log.CloseAndFlush(); | ||
} | ||
} | ||
|
||
public static void ProcessUnhandledException(Exception ex) | ||
{ | ||
try | ||
{ | ||
_log.Error(ex, null); | ||
} | ||
catch (Exception lex) | ||
{ | ||
try | ||
{ | ||
Serilog.Debugging.SelfLog.WriteLine(lex.ToString()); | ||
} | ||
catch | ||
{ | ||
// Do nothing... | ||
} | ||
} | ||
|
||
if (ex.InnerException != null) | ||
{ | ||
ProcessUnhandledException(ex.InnerException); | ||
return; | ||
} | ||
|
||
#if DEBUG | ||
MessageBox.Show(ex.ToString(), "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | ||
#else | ||
const string errorMessage = "An unexpected error ocurred. Please try again in a few minutes, and if the error persists, contact support"; | ||
MessageBox.Show(errorMessage, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | ||
#endif | ||
} | ||
|
||
private static ILogger ConfigureLogging() | ||
{ | ||
AppDomain.CurrentDomain.ProcessExit += (sender, args) => Log.CloseAndFlush(); | ||
|
||
return new LoggerConfiguration() | ||
.MinimumLevel.Verbose() | ||
.WriteTo.ExcelDnaLogDisplay(displayOrder: DisplayOrder.NewestFirst, | ||
outputTemplate: "{Properties:j}{NewLine}[{Level:u3}] {Message:lj}{NewLine}{Exception}") | ||
.Enrich.WithXllPath() | ||
.Enrich.WithExcelVersion() | ||
.Enrich.WithExcelVersionName() | ||
.Enrich.WithExcelBitness() | ||
.CreateLogger(); | ||
} | ||
|
||
private static void ApplicationThreadUnhandledException(object sender, ThreadExceptionEventArgs e) | ||
{ | ||
ProcessUnhandledException(e.Exception); | ||
} | ||
|
||
private static void TaskSchedulerUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) | ||
{ | ||
ProcessUnhandledException(e.Exception); | ||
e.SetObserved(); | ||
} | ||
|
||
private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) | ||
{ | ||
ProcessUnhandledException((Exception)e.ExceptionObject); | ||
} | ||
|
||
private static object ExcelUnhandledException(object ex) | ||
{ | ||
ProcessUnhandledException((Exception)ex); | ||
return ex; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> | ||
</startup> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("SampleAddIn")] | ||
[assembly: AssemblyDescription("")] | ||
|
||
#if DEBUG | ||
[assembly: AssemblyConfiguration("Debug")] | ||
#else | ||
[assembly: AssemblyConfiguration("Release")] | ||
#endif | ||
|
||
[assembly: AssemblyCompany("caioproiete.net")] | ||
[assembly: AssemblyProduct("Serilog.Enrichers.ExcelDna")] | ||
[assembly: AssemblyCopyright("Copyright © 2018 Caio Proiete & Contributors")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("cec178ab-3a3a-49b4-8064-d8523e0d3735")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] | ||
[assembly: AssemblyInformationalVersion("1.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="ExcelDnaProps"> | ||
<!-- | ||
If you change properties in this file, they may not come into effect until you: | ||
* Rebuild the solution/project | ||
or | ||
* Close Visual Studio | ||
* Delete .vs folder, if exists | ||
* Delete ProjectName.csproj.user (or equivalent for VB, F#, etc.), if exists | ||
* Delete SolutionName.suo, if exists | ||
* Open your solution/project again in Visual Studio | ||
--> | ||
|
||
<!-- | ||
Configuration properties for building .dna files | ||
--> | ||
<PropertyGroup> | ||
<!-- | ||
Enable/Disable automatic generation of platform-specific versions of .dna files | ||
--> | ||
<ExcelDnaCreate32BitAddIn Condition="'$(ExcelDnaCreate32BitAddIn)' == ''">true</ExcelDnaCreate32BitAddIn> | ||
<ExcelDnaCreate64BitAddIn Condition="'$(ExcelDnaCreate64BitAddIn)' == ''">true</ExcelDnaCreate64BitAddIn> | ||
|
||
<!-- | ||
Define the suffix used for each platform-specific file e.g. MyAddIn64.dna | ||
--> | ||
<ExcelDna32BitAddInSuffix Condition="'$(ExcelDna32BitAddInSuffix)' == ''">32</ExcelDna32BitAddInSuffix> | ||
<ExcelDna64BitAddInSuffix Condition="'$(ExcelDna64BitAddInSuffix)' == ''">64</ExcelDna64BitAddInSuffix> | ||
</PropertyGroup> | ||
|
||
<!-- | ||
Configuration properties for packing .dna files | ||
--> | ||
<PropertyGroup> | ||
<!-- | ||
Enable/Disable packing of .dna files | ||
--> | ||
<RunExcelDnaPack Condition="'$(RunExcelDnaPack)' == ''">false</RunExcelDnaPack> | ||
|
||
<!-- | ||
Suffix used for packed .xll files e.g. MyAddIn-packed.xll | ||
--> | ||
<ExcelDnaPackXllSuffix Condition="'$(ExcelDnaPackXllSuffix)' == ''">-packed</ExcelDnaPackXllSuffix> | ||
</PropertyGroup> | ||
</Project> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{CEC178AB-3A3A-49B4-8064-D8523E0D3735}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>SampleAddIn</RootNamespace> | ||
<AssemblyName>SampleAddIn</AssemblyName> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<Deterministic>true</Deterministic> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<DocumentationFile> | ||
</DocumentationFile> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="ExcelDna.Integration, Version=0.34.6373.42344, Culture=neutral, processorArchitecture=MSIL"> | ||
<HintPath>..\..\packages\ExcelDna.Integration.0.34.6\lib\ExcelDna.Integration.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL"> | ||
<HintPath>..\..\packages\Serilog.2.7.1\lib\net45\Serilog.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Serilog.Sinks.ExcelDnaLogDisplay, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> | ||
<HintPath>..\..\packages\Serilog.Sinks.ExcelDnaLogDisplay.1.0.0\lib\net45\Serilog.Sinks.ExcelDnaLogDisplay.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Windows.Forms" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="AddIn.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config"> | ||
<SubType>Designer</SubType> | ||
</None> | ||
<None Include="packages.config" /> | ||
<None Include="SampleAddIn.dna" /> | ||
<None Include="Properties\ExcelDna.Build.props" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Serilog.Enrichers.ExcelDna\Serilog.Enrichers.ExcelDna.csproj"> | ||
<Project>{5f26ef43-543e-48f2-8b83-52ea92f8e73c}</Project> | ||
<Name>Serilog.Enrichers.ExcelDna</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<Import Project="..\..\packages\ExcelDna.AddIn.0.34.6\tools\ExcelDna.AddIn.targets" Condition="Exists('..\..\packages\ExcelDna.AddIn.0.34.6\tools\ExcelDna.AddIn.targets')" /> | ||
<Target Name="EnsureExcelDnaTargetsImported" BeforeTargets="BeforeBuild" Condition="'$(ExcelDnaTargetsImported)' == ''"> | ||
<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" /> | ||
<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" /> | ||
</Target> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<DnaLibrary Name="Sample Add-In" RuntimeVersion="v4.0"> | ||
<ExternalLibrary Path="SampleAddIn.dll" ExplicitExports="true" ExplicitRegistration="true" LoadFromBytes="false" Pack="false" /> | ||
</DnaLibrary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="ExcelDna.AddIn" version="0.34.6" targetFramework="net45" /> | ||
<package id="ExcelDna.Integration" version="0.34.6" targetFramework="net45" /> | ||
<package id="Serilog" version="2.7.1" targetFramework="net45" /> | ||
<package id="Serilog.Sinks.ExcelDnaLogDisplay" version="1.0.0" targetFramework="net45" /> | ||
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.28010.2046 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{FC355FE6-D157-4D77-985C-8F4F0078C607}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{032590B4-A924-488D-88D9-2636B0CDE75C}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{1FB7904B-47AF-418B-8567-2CE55457A549}" | ||
ProjectSection(SolutionItems) = preProject | ||
.editorconfig = .editorconfig | ||
.gitattributes = .gitattributes | ||
.gitignore = .gitignore | ||
CHANGES.md = CHANGES.md | ||
LICENSE = LICENSE | ||
README.md = README.md | ||
EndProjectSection | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleAddIn", "sample\SampleAddIn\SampleAddIn.csproj", "{CEC178AB-3A3A-49B4-8064-D8523E0D3735}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Enrichers.ExcelDna", "src\Serilog.Enrichers.ExcelDna\Serilog.Enrichers.ExcelDna.csproj", "{5F26EF43-543E-48F2-8B83-52EA92F8E73C}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{CEC178AB-3A3A-49B4-8064-D8523E0D3735}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{CEC178AB-3A3A-49B4-8064-D8523E0D3735}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{CEC178AB-3A3A-49B4-8064-D8523E0D3735}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{CEC178AB-3A3A-49B4-8064-D8523E0D3735}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{5F26EF43-543E-48F2-8B83-52EA92F8E73C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{5F26EF43-543E-48F2-8B83-52EA92F8E73C}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{5F26EF43-543E-48F2-8B83-52EA92F8E73C}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{5F26EF43-543E-48F2-8B83-52EA92F8E73C}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(NestedProjects) = preSolution | ||
{CEC178AB-3A3A-49B4-8064-D8523E0D3735} = {FC355FE6-D157-4D77-985C-8F4F0078C607} | ||
{5F26EF43-543E-48F2-8B83-52EA92F8E73C} = {032590B4-A924-488D-88D9-2636B0CDE75C} | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {CC318D6F-08B7-4132-9850-468362A7B630} | ||
EndGlobalSection | ||
EndGlobal |
Oops, something went wrong.