diff --git a/src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs b/src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs index 7798eb38..beb6c6e4 100644 --- a/src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs +++ b/src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs @@ -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; @@ -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()) { + _client.Configuration.UseLogger(new NLogInternalLoggger()); + } + } } protected override void Write(LogEventInfo logEvent) { @@ -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)); + } } } diff --git a/src/Platforms/Exceptionless.NLog/NLogInternalLoggger.cs b/src/Platforms/Exceptionless.NLog/NLogInternalLoggger.cs new file mode 100644 index 00000000..220eb422 --- /dev/null +++ b/src/Platforms/Exceptionless.NLog/NLogInternalLoggger.cs @@ -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 + } + } +}