Skip to content

Commit deadcb6

Browse files
Initial version
1 parent 28d5dcf commit deadcb6

33 files changed

+5464
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See: https://github.com/caioproiete/serilog-sinks-exceldnalogdisplay/releases

README.md

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,109 @@
1-
# Serilog.Sinks.ExcelDnaLogDisplay
1+
# Serilog.Sinks.ExcelDnaLogDisplay [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Sinks.ExcelDnaLogDisplay.svg?style=flat)](https://www.nuget.org/packages/Serilog.Sinks.ExcelDnaLogDisplay/) [![License](https://img.shields.io/github/license/caioproiete/serilog-sinks-exceldnalogdisplay.svg)](LICENSE)
22

3-
Writes [Serilog](https://serilog.net) events to [Excel-DNA](https://excel-dna.net) LogDisplay.
3+
Writes [Serilog](https://serilog.net) events to [Excel-DNA](https://excel-dna.net) `LogDisplay`.
44

55
![Excel-DNA LogDisplay window screenshot](assets/exceldna-logdisplay-window.png)
66

7+
### Getting started
8+
9+
Install the [Serilog.Sinks.ExcelDnaLogDisplay](https://www.nuget.org/packages/Serilog.Sinks.ExcelDnaLogDisplay/) package from NuGet:
10+
11+
```powershell
12+
Install-Package Serilog.Sinks.ExcelDnaLogDisplay
13+
```
14+
15+
To configure the sink in C# code, call `WriteTo.ExcelDnaLogDisplay()` during logger configuration:
16+
17+
```csharp
18+
var log = new LoggerConfiguration()
19+
.WriteTo.ExcelDnaLogDisplay(displayOrder: DisplayOrder.NewestFirst)
20+
.CreateLogger();
21+
```
22+
### Example of an Excel-DNA add-in using Serilog with this sink
23+
24+
In the [sample](sample/) folder, there's an example of an Excel-DNA add-in that uses Serilog for logging to the `LogDisplay` of Excel-DNA using this sink, from different contexts, suchs as from an `ExcelRibbon` control event handler as well from an `ExcelFunction`:
25+
26+
[![Excel-DNA LogDisplay sample add-in ribbon screenshot](assets/sample-addin-ribbon.png)](sample/SampleAddIn/Ribbon.cs)
27+
28+
[![Excel-DNA LogDisplay sample add-in Excel function screenshot](assets/sample-addin-function.png)](sample/SampleAddIn/Functions.cs)
29+
30+
### XML `<appSettings>` configuration
31+
32+
To use the Excel-DNA LogDisplay sink with the [Serilog.Settings.AppSettings](https://github.com/serilog/serilog-settings-appsettings) package, first install that package if you haven't already done so:
33+
34+
```powershell
35+
Install-Package Serilog.Settings.AppSettings
36+
```
37+
38+
Instead of configuring the logger in code, call `ReadFrom.AppSettings()`:
39+
40+
```csharp
41+
var log = new LoggerConfiguration()
42+
.ReadFrom.AppSettings()
43+
.CreateLogger();
44+
```
45+
46+
In your Excel-DNA Add-In's `App.config`, specify the Excel-DNA LogDisplay sink assembly under the `<appSettings>` node:
47+
48+
```xml
49+
<configuration>
50+
<appSettings>
51+
<add key="serilog:using:ExcelDnaLogDisplay" value="Serilog.Sinks.ExcelDnaLogDisplay" />
52+
<add key="serilog:write-to:ExcelDnaLogDisplay" />
53+
```
54+
55+
The parameters that can be set through the `serilog:write-to:ExcelDnaLogDisplay` keys are the method parameters accepted by the `WriteTo.ExcelDnaLogDisplay()` configuration method. This means, for example, that the `displayOrder` parameter can be set with:
56+
57+
```xml
58+
<add key="serilog:write-to:ExcelDnaLogDisplay.displayOrder" value="NewestFirst" />
59+
```
60+
61+
### Controlling event formatting
62+
63+
The Excel-DNA LogDisplay sink creates events in a fixed text format by default:
64+
65+
```
66+
2018-09-07 09:02:17.148 -03:00 [INF] HTTP GET / responded 200 in 1994 ms
67+
```
68+
69+
The format is controlled using an _output template_, which the Excel-DNA LogDisplay sink configuration method accepts as an `outputTemplate` parameter.
70+
71+
The default format above corresponds to an output template like:
72+
73+
```csharp
74+
.WriteTo.ExcelDnaLogDisplay(
75+
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
76+
```
77+
78+
##### JSON event formatting
79+
80+
To write events to the Excel-DNA LogDisplay in an alternative format such as [JSON](https://github.com/serilog/serilog-formatting-compact), pass an `ITextFormatter` as the first argument:
81+
82+
```csharp
83+
// Install-Package Serilog.Formatting.Compact
84+
.WriteTo.ExcelDnaLogDisplay(new CompactJsonFormatter())
85+
```
86+
87+
### Auditing
88+
89+
The Excel-DNA LogDisplay sink can operate as an audit sink through `AuditTo`:
90+
91+
```csharp
92+
.AuditTo.ExcelDnaLogDisplay(displayOrder: DisplayOrder.NewestFirst)
93+
```
94+
95+
### Excel-DNA configuration for packing with `ExcelDnaPack`
96+
97+
In order for the Excel-DNA LogDisplay sink to work from an add-in that was packaged using the ExcelDnaPack utility, you need to include references to `Serilog.dll` and `Serilog.Sinks.ExcelDnaLogDisplay.dll` in the `.dna` file of the add-in:
98+
99+
```xml
100+
<DnaLibrary Name="My Add-In" RuntimeVersion="v4.0">
101+
<ExternalLibrary Path="MyAddIn.dll" ExplicitExports="false" LoadFromBytes="true" Pack="true" />
102+
103+
<Reference Path="Serilog.dll" Pack="true" />
104+
<Reference Path="Serilog.Sinks.ExcelDnaLogDisplay.dll" Pack="true" />
105+
```
106+
7107
---
8108

9109
_Copyright &copy; 2018 Caio Proiete & Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html)._

assets/sample-addin-function.png

13 KB
Loading

assets/sample-addin-ribbon.png

14.1 KB
Loading
18.8 KB
Loading

sample/SampleAddIn/AddIn.cs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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.Linq;
17+
using System.Runtime.CompilerServices;
18+
using System.Threading;
19+
using System.Threading.Tasks;
20+
using System.Windows.Forms;
21+
using ExcelDna.Integration;
22+
using ExcelDna.Integration.Extensibility;
23+
using ExcelDna.Logging;
24+
using ExcelDna.Registration;
25+
using Serilog;
26+
27+
namespace SampleAddIn
28+
{
29+
public class AddIn : ExcelComAddIn, IExcelAddIn
30+
{
31+
private static ILogger _log = Log.Logger;
32+
33+
public void AutoOpen()
34+
{
35+
try
36+
{
37+
Application.ThreadException += ApplicationThreadUnhandledException;
38+
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
39+
TaskScheduler.UnobservedTaskException += TaskSchedulerUnobservedTaskException;
40+
ExcelIntegration.RegisterUnhandledExceptionHandler(ExcelUnhandledException);
41+
42+
_log = Log.Logger = ConfigureLogging();
43+
_log.Information("Starting sample Excel-DNA Add-In with Serilog Sink LogDisplay");
44+
45+
ExcelComAddInHelper.LoadComAddIn(this);
46+
47+
_log.Verbose("Registering functions");
48+
49+
ExcelRegistration.GetExcelFunctions()
50+
.Select(UpdateFunctionAttributes)
51+
.RegisterFunctions();
52+
53+
_log.Information("Sample Excel-DNA Add-In with Serilog Sink LogDisplay started");
54+
}
55+
catch (Exception ex)
56+
{
57+
ProcessUnhandledException(ex);
58+
}
59+
}
60+
61+
public void AutoClose()
62+
{
63+
// Do nothing
64+
}
65+
66+
public override void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
67+
{
68+
try
69+
{
70+
base.OnDisconnection(disconnectMode, ref custom);
71+
72+
_log.Information("Stopping sample Excel-DNA Add-In with Serilog Sink LogDisplay");
73+
}
74+
catch (Exception ex)
75+
{
76+
ProcessUnhandledException(ex);
77+
}
78+
finally
79+
{
80+
_log.Information("Sample Excel-DNA Add-In with Serilog Sink LogDisplay stopped");
81+
82+
Log.CloseAndFlush();
83+
}
84+
}
85+
86+
public static void ProcessUnhandledException(Exception ex, string message = null, [CallerMemberName] string caller = null)
87+
{
88+
try
89+
{
90+
_log.Error(ex, message ?? $"Unhandled exception on {caller}");
91+
}
92+
catch (Exception lex)
93+
{
94+
try
95+
{
96+
Serilog.Debugging.SelfLog.WriteLine(lex.ToString());
97+
}
98+
catch
99+
{
100+
// Do nothing...
101+
}
102+
}
103+
104+
if (ex.InnerException != null)
105+
{
106+
ProcessUnhandledException(ex.InnerException, message, caller);
107+
return;
108+
}
109+
110+
#if DEBUG
111+
MessageBox.Show(ex.ToString(), "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
112+
#else
113+
const string errorMessage = "An unexpected error ocurred. Please try again in a few minutes, and if the error persists, contact support";
114+
MessageBox.Show(errorMessage, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
115+
#endif
116+
}
117+
118+
private static ILogger ConfigureLogging()
119+
{
120+
return new LoggerConfiguration()
121+
.MinimumLevel.Verbose()
122+
.WriteTo.ExcelDnaLogDisplay(displayOrder: DisplayOrder.NewestFirst)
123+
.CreateLogger();
124+
}
125+
126+
private static ExcelFunctionRegistration UpdateFunctionAttributes(ExcelFunctionRegistration excelFunction)
127+
{
128+
excelFunction.FunctionAttribute.Name = excelFunction.FunctionAttribute.Name.ToUpperInvariant();
129+
return excelFunction;
130+
}
131+
132+
private static void ApplicationThreadUnhandledException(object sender, ThreadExceptionEventArgs e)
133+
{
134+
ProcessUnhandledException(e.Exception);
135+
}
136+
137+
private static void TaskSchedulerUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
138+
{
139+
ProcessUnhandledException(e.Exception);
140+
e.SetObserved();
141+
}
142+
143+
private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
144+
{
145+
ProcessUnhandledException((Exception)e.ExceptionObject);
146+
}
147+
148+
private static object ExcelUnhandledException(object ex)
149+
{
150+
ProcessUnhandledException((Exception)ex);
151+
return ex;
152+
}
153+
}
154+
}

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.7.2" />
5+
</startup>
6+
</configuration>

sample/SampleAddIn/Functions.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 ExcelDna.Integration;
16+
using Serilog;
17+
18+
namespace SampleAddIn
19+
{
20+
public static class Functions
21+
{
22+
private static readonly ILogger _log = Log.Logger.ForContext(typeof(Functions));
23+
24+
[ExcelFunction(Category = "Serilog", Description = "Writes a `Verbose` message to the LogDisplay via Serilog")]
25+
public static string LogVerbose(string message)
26+
{
27+
_log.Verbose(message);
28+
return $"'[VRB] {message}' written to the log";
29+
}
30+
31+
[ExcelFunction(Category = "Serilog", Description = "Writes a `Debug` message to the LogDisplay via Serilog")]
32+
public static string LogDebug(string message)
33+
{
34+
_log.Debug(message);
35+
return $"'[DBG] {message}' written to the log";
36+
}
37+
38+
[ExcelFunction(Category = "Serilog", Description = "Writes an `Information` message to the LogDisplay via Serilog")]
39+
public static string LogInformation(string message)
40+
{
41+
_log.Information(message);
42+
return $"'[INF] {message}' written to the log";
43+
}
44+
45+
[ExcelFunction(Category = "Serilog", Description = "Writes a `Warning` message to the LogDisplay via Serilog")]
46+
public static string LogWarning(string message)
47+
{
48+
_log.Warning(message);
49+
return $"'[WRN] {message}' written to the log";
50+
}
51+
52+
[ExcelFunction(Category = "Serilog", Description = "Writes an `Error` message to the LogDisplay via Serilog")]
53+
public static string LogError(string message)
54+
{
55+
_log.Error(message);
56+
return $"'[ERR] {message}' written to the log";
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)