Skip to content

Commit

Permalink
NLog ExceptionlessTarget: ProcessQueueAsync for ExceptionlessClient o…
Browse files Browse the repository at this point in the history
…n Flush (#301)

* ExceptionlessTarget - ProcessQueueAsync for ExceptionlessClient on Flush

* ExceptionlessTarget - Added NLogInternalLoggger for easier troubleshooting
  • Loading branch information
snakefoot authored Apr 21, 2023
1 parent b7cdc10 commit 9e24a9c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using Exceptionless.Dependency;
using NLog;
using NLog.Common;
using NLog.Config;
using NLog.Layouts;
using NLog.Targets;
Expand Down Expand Up @@ -29,14 +31,21 @@ protected override void InitializeTarget() {
var apiKey = RenderLogEvent(ApiKey, LogEventInfo.CreateNullEvent());
var serverUrl = RenderLogEvent(ServerUrl, LogEventInfo.CreateNullEvent());

if (!String.IsNullOrEmpty(apiKey) || !String.IsNullOrEmpty(serverUrl))
if (!String.IsNullOrEmpty(apiKey) || !String.IsNullOrEmpty(serverUrl)) {
_client = new ExceptionlessClient(config => {
if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE")
config.ApiKey = apiKey;
if (!String.IsNullOrEmpty(serverUrl))
config.ServerUrl = serverUrl;
config.UseLogger(new NLogInternalLoggger());
config.UseInMemoryStorage();
});
}
else {
if (_client.Configuration.Resolver.HasDefaultRegistration<Logging.IExceptionlessLog, Logging.NullExceptionlessLog>()) {
_client.Configuration.UseLogger(new NLogInternalLoggger());
}
}
}

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

builder.Submit();
}

protected override void FlushAsync(AsyncContinuation asyncContinuation) {
_client.ProcessQueueAsync().ContinueWith(t => asyncContinuation(t.Exception));
}
}
}
36 changes: 36 additions & 0 deletions src/Platforms/Exceptionless.NLog/NLogInternalLoggger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Exceptionless.Logging;
using NLog.Common;

namespace Exceptionless.NLog {
internal class NLogInternalLoggger : IExceptionlessLog {
public LogLevel MinimumLogLevel { get; set; }

public void Debug(string message, string source = null) {
InternalLogger.Debug("ExceptionLess: {0} Source={1}", message, source);
}

public void Error(string message, string source = null, Exception exception = null) {
if (exception is null)
InternalLogger.Error("ExceptionLess: {0} Source={1}", message, source);
else
InternalLogger.Error(exception, "ExceptionLess: {0} Source={1}", message, source);
}

public void Info(string message, string source = null) {
InternalLogger.Info("ExceptionLess: {0} Source={1}", message, source);
}

public void Trace(string message, string source = null) {
InternalLogger.Trace("ExceptionLess: {0} Source={1}", message, source);
}

public void Warn(string message, string source = null) {
InternalLogger.Warn("ExceptionLess: {0} Source={1}", message, source);
}

public void Flush() {
// NLog InternalLogger has no flush
}
}
}

0 comments on commit 9e24a9c

Please sign in to comment.