Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is FlushAsync (https://nlog-project.org/documentation/v4.4.0/html/M_NLog_Targets_Target_FlushAsync.htm) called during shutdown or periodically during buffering? If it's while it's buffering, then we probably should remove this as that's already taken care of.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NLog only calls Flush on process-exit, or if the user explict calls NLog.LogManager.Flush() or NLog.LogManager.Shutdown()

}
}
}
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
}
}
}