From b7cdc101ba0dc3082720e669d3bc5cc3197a2af2 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Tue, 18 Apr 2023 19:14:23 +0200 Subject: [PATCH] NLog ExceptionlessTarget: ApiKey and ServerUrl and Message and UserIdentity with Layout support (#299) * ExceptionlessTarget - ApiKey and ServerUrl and Message and and UserIdentity and ReferenceId with Layout support * Resolved review comments from pull-request * Added support for UserIdentityName + UserIdentityEmail * Removed support for UserIdentityEmail --- .../ExceptionlessClientExtensions.cs | 17 +++++----- .../Exceptionless.NLog/ExceptionlessTarget.cs | 31 +++++++++++++------ .../LogEventBuilderExtensions.cs | 8 ++--- .../NLogExceptionlessLog.cs | 11 +++---- src/Platforms/Exceptionless.NLog/readme.txt | 5 ++- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/Platforms/Exceptionless.NLog/ExceptionlessClientExtensions.cs b/src/Platforms/Exceptionless.NLog/ExceptionlessClientExtensions.cs index 112a5ccd..401dd801 100644 --- a/src/Platforms/Exceptionless.NLog/ExceptionlessClientExtensions.cs +++ b/src/Platforms/Exceptionless.NLog/ExceptionlessClientExtensions.cs @@ -8,6 +8,10 @@ namespace Exceptionless.NLog { public static class ExceptionlessClientExtensions { public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, LogEventInfo ev) { + return CreateFromLogEvent(client, ev, ev.FormattedMessage); + } + + public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, LogEventInfo ev, string formattedMessage) { if (client == null) throw new ArgumentNullException(nameof(client)); @@ -20,10 +24,10 @@ public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, L builder.Target.Date = ev.TimeStamp; builder.SetSource(ev.LoggerName); - if (ev.Properties.Count > 0) { + if (ev.HasProperties) { foreach (var property in ev.Properties) { string propertyKey = property.Key.ToString(); - if (_ignoredEventProperties.Contains(propertyKey, StringComparer.OrdinalIgnoreCase)) + if (_ignoredEventProperties.Contains(propertyKey)) continue; if (propertyKey.Equals("@value", StringComparison.OrdinalIgnoreCase)) { @@ -53,8 +57,8 @@ public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, L builder.SetType(Event.KnownTypes.Error); } - if (!String.IsNullOrWhiteSpace(ev.FormattedMessage)) - builder.SetMessage(ev.FormattedMessage); + if (!String.IsNullOrWhiteSpace(formattedMessage)) + builder.SetMessage(formattedMessage); var tags = ev.GetTags(); if (tags != null) @@ -70,10 +74,7 @@ public static void SubmitFromLogEvent(this ExceptionlessClient client, LogEventI CreateFromLogEvent(client, ev).Submit(); } - private static readonly List _ignoredEventProperties = new List { - "CallerFilePath", - "CallerMemberName", - "CallerLineNumber", + private static readonly HashSet _ignoredEventProperties = new HashSet(StringComparer.OrdinalIgnoreCase) { "Tags", "ContextData" }; diff --git a/src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs b/src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs index 16e516fa..7798eb38 100644 --- a/src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs +++ b/src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using NLog; using NLog.Config; +using NLog.Layouts; using NLog.Targets; namespace Exceptionless.NLog { @@ -9,25 +10,31 @@ namespace Exceptionless.NLog { public class ExceptionlessTarget : TargetWithLayout { private ExceptionlessClient _client = ExceptionlessClient.Default; - public string ApiKey { get; set; } - public string ServerUrl { get; set; } + public Layout ApiKey { get; set; } + public Layout ServerUrl { get; set; } + public Layout UserIdentity { get; set; } + public Layout UserIdentityName { get; set; } [ArrayParameter(typeof(ExceptionlessField), "field")] public IList Fields { get; set; } public ExceptionlessTarget() { Fields = new List(); + Layout = "${message}"; } protected override void InitializeTarget() { base.InitializeTarget(); - if (!String.IsNullOrEmpty(ApiKey) || !String.IsNullOrEmpty(ServerUrl)) + var apiKey = RenderLogEvent(ApiKey, LogEventInfo.CreateNullEvent()); + var serverUrl = RenderLogEvent(ServerUrl, LogEventInfo.CreateNullEvent()); + + 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; + if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE") + config.ApiKey = apiKey; + if (!String.IsNullOrEmpty(serverUrl)) + config.ServerUrl = serverUrl; config.UseInMemoryStorage(); }); } @@ -40,9 +47,15 @@ protected override void Write(LogEventInfo logEvent) { if (logEvent.Level < minLogLevel) return; - var builder = _client.CreateFromLogEvent(logEvent); + var formattedMessage = RenderLogEvent(Layout, logEvent); + var builder = _client.CreateFromLogEvent(logEvent, formattedMessage); + + var userIdentity = RenderLogEvent(UserIdentity, logEvent); + var userIdentityName = RenderLogEvent(UserIdentityName, logEvent); + builder.Target.SetUserIdentity(userIdentity, userIdentityName); + foreach (var field in Fields) { - var renderedField = field.Layout.Render(logEvent); + var renderedField = RenderLogEvent(field.Layout, logEvent); if (!String.IsNullOrWhiteSpace(renderedField)) builder.AddObject(renderedField, field.Name); } diff --git a/src/Platforms/Exceptionless.NLog/LogEventBuilderExtensions.cs b/src/Platforms/Exceptionless.NLog/LogEventBuilderExtensions.cs index 94033233..589fa6dd 100644 --- a/src/Platforms/Exceptionless.NLog/LogEventBuilderExtensions.cs +++ b/src/Platforms/Exceptionless.NLog/LogEventBuilderExtensions.cs @@ -90,8 +90,8 @@ public static LogEventBuilder MarkUnhandled(this LogEventBuilder builder, string } internal static HashSet GetTags(this LogEventInfo ev) { - if (ev.Properties.ContainsKey(Tags) && ev.Properties[Tags] is HashSet) - return (HashSet)ev.Properties[Tags]; + if (ev.HasProperties && ev.Properties.TryGetValue(Tags, out var tags)) + return tags as HashSet; return null; } @@ -109,8 +109,8 @@ private static void AddTags(this LogEventInfo ev, params string[] tags) { } internal static IDictionary GetContextData(this LogEventInfo ev) { - if (ev.Properties.ContainsKey(ContextData) && ev.Properties[ContextData] is IDictionary) - return (IDictionary)ev.Properties[ContextData]; + if (ev.HasProperties && ev.Properties.TryGetValue(ContextData, out var contextData)) + return contextData as IDictionary; return null; } diff --git a/src/Platforms/Exceptionless.NLog/NLogExceptionlessLog.cs b/src/Platforms/Exceptionless.NLog/NLogExceptionlessLog.cs index 0ed4b830..b9c1422a 100644 --- a/src/Platforms/Exceptionless.NLog/NLogExceptionlessLog.cs +++ b/src/Platforms/Exceptionless.NLog/NLogExceptionlessLog.cs @@ -1,7 +1,6 @@ using System; using Exceptionless.Logging; using NLog; -using NLog.Fluent; using LogLevel = Exceptionless.Logging.LogLevel; namespace Exceptionless.NLog { @@ -19,35 +18,35 @@ public void Error(string message, string source = null, Exception exception = nu if (LogLevel.Error < MinimumLogLevel) return; - _logger.ForErrorEvent().Message(message).LoggerName(source).Exception(exception).Log(); + _logger.ForErrorEvent().Message(message).LoggerName(source).Exception(exception).Log(typeof(NLogExceptionlessLog)); } public void Info(string message, string source = null) { if (LogLevel.Info < MinimumLogLevel) return; - _logger.ForInfoEvent().Message(message).LoggerName(source).Log(); + _logger.ForInfoEvent().Message(message).LoggerName(source).Log(typeof(NLogExceptionlessLog)); } public void Debug(string message, string source = null) { if (LogLevel.Debug < MinimumLogLevel) return; - _logger.ForDebugEvent().Message(message).LoggerName(source).Log(); + _logger.ForDebugEvent().Message(message).LoggerName(source).Log(typeof(NLogExceptionlessLog)); } public void Warn(string message, string source = null) { if (LogLevel.Warn < MinimumLogLevel) return; - _logger.ForWarnEvent().Message(message).LoggerName(source).Log(); + _logger.ForWarnEvent().Message(message).LoggerName(source).Log(typeof(NLogExceptionlessLog)); } public void Trace(string message, string source = null) { if (LogLevel.Trace < MinimumLogLevel) return; - _logger.ForTraceEvent().Message(message).LoggerName(source).Log(); + _logger.ForTraceEvent().Message(message).LoggerName(source).Log(typeof(NLogExceptionlessLog)); } public void Flush() { } diff --git a/src/Platforms/Exceptionless.NLog/readme.txt b/src/Platforms/Exceptionless.NLog/readme.txt index 79361d6c..a958c845 100644 --- a/src/Platforms/Exceptionless.NLog/readme.txt +++ b/src/Platforms/Exceptionless.NLog/readme.txt @@ -37,11 +37,10 @@ minimum log level that will be used until the client retrieves settings from the -    +          -      -            +