Skip to content

Commit d3f15bf

Browse files
committed
Make the log4net appender work better.
1 parent af26b1b commit d3f15bf

File tree

6 files changed

+35
-10
lines changed

6 files changed

+35
-10
lines changed

Source/Platforms/Log4net/ExceptionlessAppender.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Exceptionless.Log4net {
66
public class ExceptionlessAppender : AppenderSkeleton {
7-
private ExceptionlessClient _client;
7+
private ExceptionlessClient _client = ExceptionlessClient.Default;
88

99
public string ApiKey { get; set; }
1010
public string ServerUrl { get; set; }
@@ -18,8 +18,6 @@ public override void ActivateOptions() {
1818
config.ServerUrl = ServerUrl;
1919
config.UseInMemoryStorage();
2020
});
21-
else
22-
_client = ExceptionlessClient.Default;
2321
}
2422

2523
protected override void Append(LoggingEvent loggingEvent) {

Source/Platforms/Log4net/ExceptionlessClientExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, L
1616
builder.SetSource(ev.LoggerName);
1717

1818
var props = ev.GetProperties();
19-
foreach (var key in props.GetKeys().Where(key => !_ignoredEventProperties.Contains(key, StringComparer.OrdinalIgnoreCase)))
20-
builder.SetProperty(key, props[key]);
19+
foreach (var key in props.GetKeys().Where(key => !_ignoredEventProperties.Contains(key, StringComparer.OrdinalIgnoreCase))) {
20+
string propName = key;
21+
if (propName.StartsWith("log4net:"))
22+
propName = propName.Substring(8);
23+
builder.SetProperty(propName, props[key]);
24+
}
2125

2226
return builder;
2327
}

Source/Platforms/Log4net/NuGet/readme.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ and viewing your project configuration page.
2020

2121
Here is an example log.config file that shows how to use the Exceptionless log4net appender.
2222

23-
<appender name="ExceptionlessAppender" type="Exceptionless.Log4net.ExceptionlessAppender, Exceptionless.Log4net">
23+
<appender name="exceptionless" type="Exceptionless.Log4net.ExceptionlessAppender,Exceptionless.Log4net" />
24+
25+
By default, the appender will use the settings from the Exceptionless config section, but
26+
you can also set it on the appender like this:
27+
28+
<appender name="exceptionless" type="Exceptionless.Log4net.ExceptionlessAppender, Exceptionless.Log4net">
2429
<apiKey value="API_KEY_HERE" />
2530
</appender>
26-
2731
-------------------------------------
2832
Documentation and Support
2933
-------------------------------------

Source/Platforms/NLog/NuGet/readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ and viewing your project configuration page.
1818
NLog Integration
1919
-------------------------------------
2020

21-
Here is an example NLog.config file that shows how to use the Exceptionless NLog target.
21+
Here is an example NLog.config file that shows how to use the Exceptionless NLog target. The apiKey attribute
22+
is optional and will be picked up from your Exceptionless config section by default.
2223

2324
<nlog>
2425
<extensions>

Source/Samples/SampleConsole/App.config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<configuration>
33
<configSections>
44
<section name="exceptionless" type="Exceptionless.ExceptionlessSection, Exceptionless.Extras"/>
5+
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
56
</configSections>
67

78
<exceptionless apiKey="LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw" serverUrl="http://localhost:50000" enableSSL="false" tags="Tag1,Tag2" enableLogging="true">
@@ -18,6 +19,19 @@
1819
</registrations>-->
1920
</exceptionless>
2021

22+
<log4net>
23+
<appender name="exceptionless" type="Exceptionless.Log4net.ExceptionlessAppender,Exceptionless.Log4net">
24+
<layout type="log4net.Layout.PatternLayout">
25+
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
26+
</layout>
27+
</appender>
28+
29+
<root>
30+
<level value="DEBUG" />
31+
<appender-ref ref="exceptionless" />
32+
</root>
33+
</log4net>
34+
2135
<startup>
2236
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
2337
</startup>

Source/Samples/SampleConsole/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using Exceptionless.Models;
2525
using log4net;
2626
using log4net.Config;
27+
using log4net.Core;
2728
using NLog.Fluent;
2829

2930
namespace SampleConsole {
@@ -55,10 +56,13 @@ private static void Main() {
5556
ExceptionlessClient.Default.Register();
5657

5758
// test NLog
58-
Log.Info().Message("Hi").Write();
59+
NLog.GlobalDiagnosticsContext.Set("GlobalProp", "GlobalValue");
60+
Log.Info().Message("Hi").Property("LocalProp", "LocalValue").Write();
5961

6062
// test log4net
61-
BasicConfigurator.Configure(new ExceptionlessAppender());
63+
XmlConfigurator.Configure();
64+
GlobalContext.Properties["GlobalProp"] = "GlobalValue";
65+
ThreadContext.Properties["LocalProp"] = "LocalValue";
6266
_log4net.Info("Hi");
6367

6468
var tokenSource = new CancellationTokenSource();

0 commit comments

Comments
 (0)