Skip to content

Commit e09b635

Browse files
committed
Add NLog package and example configuration
1 parent 50038a8 commit e09b635

File tree

6 files changed

+52
-18
lines changed

6 files changed

+52
-18
lines changed

Logging/Log4NetTraceListener.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// https://code.google.com/p/cavity/source/browse/trunk/src/Cavity%20Diagnostics%20log4net/Class%20Libraries/Diagnostics.Log4Net/Diagnostics/Log4NetTraceListener.cs
44
// Code License: MIT License (http://www.opensource.org/licenses/mit-license.php)
55

6+
// Source for similar implementation targeting NLog (included in the NLog assembly) is here: https://github.com/jkowalski/NLog/blob/master/src/NLog/NLogTraceListener.cs
7+
68
namespace Cavity.Diagnostics
79
{
810
using System;

Logging/Logging-AddIn.xll.config

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,22 @@
44
<!-- This .config file contains the various configuration examples.
55
66
log4net
7-
7+
NLog
88
99
-->
1010

1111
<!-- ============= Begin log4net ================ -->
1212

13+
<!--
1314
<configSections>
1415
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
1516
</configSections>
1617
<system.diagnostics>
1718
<sources>
18-
<!-- By default the TraceSource is configured to source only Warning, Error and Fatal.
19-
the configuration can override this by setting switchValue, or create a SourceSwitch.
20-
switchValue is a SourceLevel: Off, Critical, Error, Warning (default), Information, Verbose, All
21-
By default, LogDisplay is only shown if Errors are present.
22-
-->
2319
<source name="ExcelDna.Integration" switchValue="Verbose">
2420
<listeners>
2521
<remove name="Default" />
26-
<!--<clear/>-->
2722
<add name="LogDisplay" type="ExcelDna.Logging.LogDisplayTraceListener,ExcelDna.Integration">
28-
<!-- EventTypeFilter takes a SourceLevel: Off, Critical, Error, Warning (default), Information, Verbose, All -->
29-
<!-- We use the filter here to disable the LogDisplay -->
3023
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Off"/>
3124
</add>
3225
<add name="Log4NetListener" type="Cavity.Diagnostics.Log4NetTraceListener,Logging" >
@@ -36,10 +29,6 @@
3629
</source>
3730
</sources>
3831
</system.diagnostics>
39-
<appSettings>
40-
<!-- Change this setting to "true" if you want to debug the log4net configuration -->
41-
<add key="log4net.Internal.Debug" value="false"/>
42-
</appSettings>
4332
<log4net>
4433
<appender name="DebugAppender" type="log4net.Appender.DebugAppender" >
4534
<layout type="log4net.Layout.PatternLayout">
@@ -51,7 +40,38 @@
5140
<appender-ref ref="DebugAppender" />
5241
</root>
5342
</log4net>
54-
55-
<!-- ============= End log4net ================ -->
43+
-->
5644

45+
<!-- ============= End log4net ================ -->
46+
47+
<!-- ============= Begin NLog ================ -->
48+
49+
<configSections>
50+
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
51+
</configSections>
52+
<system.diagnostics>
53+
<sources>
54+
<source name="ExcelDna.Integration" switchValue="Verbose">
55+
<listeners>
56+
<remove name="Default" />
57+
<add name="LogDisplay" type="ExcelDna.Logging.LogDisplayTraceListener,ExcelDna.Integration">
58+
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Off"/>
59+
</add>
60+
<add name="NLogListener" type="NLog.NLogTraceListener,NLog" >
61+
<filter type="System.Diagnostics.EventTypeFilter" initializeData="All"/>
62+
</add>
63+
</listeners>
64+
</source>
65+
</sources>
66+
</system.diagnostics>
67+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
68+
<targets>
69+
<target name="debugger" xsi:type="Debugger" layout="NLOG: ${longdate}|${level:uppercase=true}|${logger}[${event-properties:EventID}]|${message}"/>
70+
</targets>
71+
<rules>
72+
<logger name="*" minlevel="Warn" writeTo="debugger" />
73+
</rules>
74+
</nlog>
75+
76+
<!-- ============= End NLog ================ -->
5777
</configuration>

Logging/Logging.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
<Reference Include="log4net">
3838
<HintPath>packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath>
3939
</Reference>
40+
<Reference Include="NLog">
41+
<HintPath>packages\NLog.4.0.1\lib\net40\NLog.dll</HintPath>
42+
</Reference>
4043
<Reference Include="System" />
4144
<Reference Include="System.Core" />
4245
<Reference Include="System.Xml.Linq" />

Logging/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Excel-DNA Logging Samples
22
=========================
33

44
The Logging samples show different ways of configuring the Excel-DNA diagnostic logging.
5+
To examine each option, uncomment the corresponding part in the Logging-AddIn.xll.config file.
56

67
### log4net
7-
A Log4Net TraceListener is defined, which forwards trace events to Log4Net.
8+
A Log4Net TraceListener is defined (in this example project), that forwards trace events to Log4Net.
9+
10+
### NLog
11+
The NLog library includes an NLogTraceListener that can be configured to forward trace events to NLog.

Logging/TestLoggers.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
using System.Linq;
44
using System.Text;
55
using ExcelDna.Integration;
6+
67
using log4net;
78
using log4net.Config;
89

10+
using NLog;
11+
912
[assembly: log4net.Config.XmlConfigurator()]
1013
namespace Logging
1114
{
@@ -20,12 +23,13 @@ public void AutoOpen()
2023
// Alternative is to call
2124
// XmlConfigurator.Configure();
2225
// but for our example it would be too late.
23-
ILog log = LogManager.GetLogger(typeof(TestLoggers)); // Typically
26+
ILog log = log4net.LogManager.GetLogger(typeof(TestLoggers)); // Typically
2427
log.Info("Testing log4net Info message");
2528

2629

2730
// NLog
28-
31+
Logger logger = NLog.LogManager.GetLogger("TestLogger");
32+
logger.Info("Testing NLog Info message");
2933
}
3034

3135
public void AutoClose() { }

Logging/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<package id="Excel-DNA" version="0.32.0" targetFramework="net40" />
44
<package id="Excel-DNA.Lib" version="0.32.0" targetFramework="net40" />
55
<package id="log4net" version="2.0.3" targetFramework="net40" />
6+
<package id="NLog" version="4.0.1" targetFramework="net40" />
67
</packages>

0 commit comments

Comments
 (0)