Skip to content

Commit 9e24a9c

Browse files
authored
NLog ExceptionlessTarget: ProcessQueueAsync for ExceptionlessClient on Flush (#301)
* ExceptionlessTarget - ProcessQueueAsync for ExceptionlessClient on Flush * ExceptionlessTarget - Added NLogInternalLoggger for easier troubleshooting
1 parent b7cdc10 commit 9e24a9c

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using Exceptionless.Dependency;
34
using NLog;
5+
using NLog.Common;
46
using NLog.Config;
57
using NLog.Layouts;
68
using NLog.Targets;
@@ -29,14 +31,21 @@ protected override void InitializeTarget() {
2931
var apiKey = RenderLogEvent(ApiKey, LogEventInfo.CreateNullEvent());
3032
var serverUrl = RenderLogEvent(ServerUrl, LogEventInfo.CreateNullEvent());
3133

32-
if (!String.IsNullOrEmpty(apiKey) || !String.IsNullOrEmpty(serverUrl))
34+
if (!String.IsNullOrEmpty(apiKey) || !String.IsNullOrEmpty(serverUrl)) {
3335
_client = new ExceptionlessClient(config => {
3436
if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE")
3537
config.ApiKey = apiKey;
3638
if (!String.IsNullOrEmpty(serverUrl))
3739
config.ServerUrl = serverUrl;
40+
config.UseLogger(new NLogInternalLoggger());
3841
config.UseInMemoryStorage();
3942
});
43+
}
44+
else {
45+
if (_client.Configuration.Resolver.HasDefaultRegistration<Logging.IExceptionlessLog, Logging.NullExceptionlessLog>()) {
46+
_client.Configuration.UseLogger(new NLogInternalLoggger());
47+
}
48+
}
4049
}
4150

4251
protected override void Write(LogEventInfo logEvent) {
@@ -62,5 +71,9 @@ protected override void Write(LogEventInfo logEvent) {
6271

6372
builder.Submit();
6473
}
74+
75+
protected override void FlushAsync(AsyncContinuation asyncContinuation) {
76+
_client.ProcessQueueAsync().ContinueWith(t => asyncContinuation(t.Exception));
77+
}
6578
}
6679
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using Exceptionless.Logging;
3+
using NLog.Common;
4+
5+
namespace Exceptionless.NLog {
6+
internal class NLogInternalLoggger : IExceptionlessLog {
7+
public LogLevel MinimumLogLevel { get; set; }
8+
9+
public void Debug(string message, string source = null) {
10+
InternalLogger.Debug("ExceptionLess: {0} Source={1}", message, source);
11+
}
12+
13+
public void Error(string message, string source = null, Exception exception = null) {
14+
if (exception is null)
15+
InternalLogger.Error("ExceptionLess: {0} Source={1}", message, source);
16+
else
17+
InternalLogger.Error(exception, "ExceptionLess: {0} Source={1}", message, source);
18+
}
19+
20+
public void Info(string message, string source = null) {
21+
InternalLogger.Info("ExceptionLess: {0} Source={1}", message, source);
22+
}
23+
24+
public void Trace(string message, string source = null) {
25+
InternalLogger.Trace("ExceptionLess: {0} Source={1}", message, source);
26+
}
27+
28+
public void Warn(string message, string source = null) {
29+
InternalLogger.Warn("ExceptionLess: {0} Source={1}", message, source);
30+
}
31+
32+
public void Flush() {
33+
// NLog InternalLogger has no flush
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)